Skip to content

Instantly share code, notes, and snippets.

@emihir0
Created April 20, 2022 14:05
Show Gist options
  • Save emihir0/091e6422522e66549aaad69944952e8b to your computer and use it in GitHub Desktop.
Save emihir0/091e6422522e66549aaad69944952e8b to your computer and use it in GitHub Desktop.
import requests
class GiritonApi:
TOKEN = 'xxx'
URL = 'https://rest.giriton.com/system/api/'
ENDPOINT_EMPLOYEES_LIST = 'hr/usersEmployedOn'
ENDPOINT_EMPLOYEE = 'hr/userEmployedOn'
FIELD_EMPLOYEE_ID = 'number'
FIELD_PRESENCE_CARD_IDS = 'presenceCardIds'
@property
def headers(self):
return {'Content-Type': 'application/json', 'giriton-TOKEN': self.TOKEN}
def _construct_url(self, endpoint):
return f'{self.URL}{endpoint}'
def get(self, endpoint):
url = self._construct_url(endpoint)
return requests.get(url, headers=self.headers)
def post(self, endpoint, data):
url = self._construct_url(endpoint)
return requests.post(url, json=data, headers=self.headers)
def get_employees_list(self):
response = self.get(self.ENDPOINT_EMPLOYEES_LIST)
return response.json()['entries']
def get_employee(self, employee_id):
# Giriton does not allow to fetch a user with a `number`, hence we fetch all employees and then filter
employees = self.get_employees_list()
for e in employees:
if e[self.FIELD_EMPLOYEE_ID] == f'{employee_id}':
return e
raise KeyError
def update_employee(self, employee_id, data):
response = self.post(self.ENDPOINT_EMPLOYEE, data={self.FIELD_EMPLOYEE_ID: f'{employee_id}', **data})
return response
api = GiritonApi()
cards = ['987654321654987', '654321']
r = api.update_employee(201, data={api.FIELD_PRESENCE_CARD_IDS: cards})
assert r.status_code == 200
emp = api.get_employee(employee_id=201)
assert emp[api.FIELD_PRESENCE_CARD_IDS] == ['987654321654987', '654321']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment