Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from exceptions import *
- from json import loads as _loads
- import requests
- import datetime
- PRIVATE_COMMANDS = ['whoAmI']
- class FikenConnectError(Exception):
- def __init__(self, err):
- if err == 415:
- raise HTTP415
- elif err == 405:
- raise HTTP405
- elif err == 404:
- raise HTTP404
- elif err == 403:
- raise HTTP403
- elif err == 401:
- raise HTTP401
- elif err == 400:
- raise HTTP400
- class FikenConnect(object):
- def __init__(self,
- username=None,
- password=None,
- timeout=5,
- debug_endpoint=False):
- self.username = str(username) if username else None
- self.password = str(password) if password else None
- self.timeout = timeout
- self.debug_endpoint = debug_endpoint
- self.base_url = 'https://fiken.no/api/v1/'
- self.data = None
- self.filtered_data = None
- self.session = requests.Session()
- def __call__(self, command):
- self.base_url = 'https://fiken.no/api/v1/'
- if command in PRIVATE_COMMANDS:
- if not self.username or not self.password:
- raise FikenConnectError("Username and Password needed!")
- url = self.base_url + '{}?'.format(command)
- if self.debug_endpoint:
- print(url)
- headers = {
- 'Content Type': 'application/json',
- 'Accept': 'application/hal+json'
- }
- self.session.auth = (self.username, self.password)
- self.session.headers = headers
- # post request
- ret = self.session.get(url, timeout=self.timeout)
- if ret.status_code != 200:
- raise FikenConnectError("Status Code: %s" % ret.status_code)
- return None
- else:
- json_out = _loads(ret.content.decode('utf-8'))
- return json_out
- def test_connection(self):
- self.data = self.__call__('whoAmI')
- return self.data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement