-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
51 lines (45 loc) · 1.34 KB
/
lib.py
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
40
41
42
43
44
45
46
47
48
49
50
51
import hashlib
import click
import requests
import json
import sys
from config import *
def request_body(type, attributes):
return { 'data': {
'type': type,
'attributes': attributes
}
}
def store_session(token):
filename = hashlib.sha1(BASE_URL.encode('utf-8')).hexdigest() + '_session_id.key'
f = open(filename, 'w')
f.write(token)
f.close()
def fetch_session():
try:
filename = hashlib.sha1(BASE_URL.encode('utf-8')).hexdigest() + '_session_id.key'
f = open(filename, 'r')
token = f.read()
f.close()
return token
except IOError:
print("Not token could be retrieved.")
print("Please start a session using 'start-session' command.")
sys.exit(2)
def fetch_file(filename):
f = open(filename, 'r')
code = f.read()
f.close()
return code
def authentified_headers():
token = fetch_session()
return {
'AUTHORIZATION': 'Bearer ' + token,
'CONTENT_TYPE': 'application/vnd.splay+json; version=1'
}
def check_response(response):
if response.status_code != 200:
click.echo("Error code : " + str(response.status_code))
click.echo("Message : " + response.json()['errors'])
click.echo("Contact administrator or review command usage with --help option")
sys.exit(1)