aboutsummaryrefslogtreecommitdiff
path: root/wikijscmd/custom_requests.py
blob: a32a14c9e319e5fc378a88f63774591984249786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import requests
import sys
import json
from wikijscmd.config import config

def handle_errors(r):
    error = False
    if r.status_code != 200:
        error = True
        print("Error status code: %s" % r.status_code)
    json = r.json()
    if "errors" in json:
        error = True
        for e in json["errors"]:
            print(e["message"])
            print(e)
    if error:
        sys.exit(1)


def get_headers():
    return { "Authorization": "Bearer %s" % config["wiki"]["key"] } 

def send_query(query, query_vars):
    '''Returns status code, json'''
    payload = { "query": query, "variables": query_vars}
    r = requests.post(config["wiki"]["url"], json=payload, headers = get_headers())
    handle_errors(r)
    return r.json()

def query_date(date):
    res = requests.get(
        f'{config["tracker"]["url"]}/{date}',
        auth=requests.auth.HTTPBasicAuth(
            config["tracker"]["username"], config["tracker"]["password"])
    )
    if res.status_code != 200:
        return []
    return res.json()