Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_file_contents(path):
- try:
- with open('token') as infile:
- return infile.read().strip()
- except FileNotFoundError:
- print("Couldn't find the token file!")
- return None
- class ByiloApiClient(object):
- token = None
- session = requests.Session()
- def __init__(self, api_url):
- self.api_url = api_url
- self.login()
- self.token = self.load_token()
- self.headers = {'content-type': 'application/json', 'token': self.token}
- if self.token is None:
- print('Authentication is failed')
- else:
- print('Authentication is successful Token is present.')
- # Authorization
- def login(self):
- username = 'kravchuk@gmail.com'
- password = 'hjb567HgMjylh'
- headers = {'content-type': 'application/json'}
- payload = {
- 'email': username,
- 'password': password
- }
- r = requests.post(self.api_url + '/auth/admin',
- data=json.dumps(payload),
- headers=headers)
- r.raise_for_status()
- self.token = r.json()['token']
- self.save_token(self.token)
- self.session.headers.update({'Authorization': self.token})
- # GET Constraction standarts
- def get_cs(self):
- r = requests.get(self.api_url + '/cs', headers=self.headers)
- r.raise_for_status()
- return r.content
- # GET Clients
- def get_client(self):
- headers = {'content-type': 'application/json', 'token': self.token}
- payload = {'id': 'client id', 'name': 'client name', 'csCount': 'amount of assigned construction standards',
- 'activeUserCount': 'amount of active users'}
- r = requests.get(self.api_url + '/client', headers=self.headers, params=payload)
- r.raise_for_status()
- return r.text
- # POST Client
- def post_client(self):
- json = {'code': 'EN-1993', 'limit': '3','c': '3', 'end': '2016-09-24T16:49:45.415Z'}'
- data = {'name': datetime.datetime.now(), 'csCount': '1', 'code': 'SC', 'email': 'sc@gmail.com', 'phone': '+304422222222', 'country':'Ukraine',
- 'city': 'Kiev', 'postcode': '22222', 'address1': 'mazepa str., 2', 'address2': 'petlura str., 2', "csInfo": json }
- r = requests.post(self.api_url + 'client', headers=self.headers, params=data)
- return r.text
- def load_token(self):
- return read_file_contents('token')
- def save_token(self, str):
- with open('token', 'w') as outfile:
- outfile.write(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement