def delete(self, group_id): """ Delete the object. DELETE /objectGroups/{groupId} https://bbdata.daplab.ch/api/#objectgroups__groupid__delete """ url = output_api_url + self.base_path + "/" + str(group_id) r = requests.delete(url, headers=self.auth.headers) handle_response(r.status_code, True)
def __aggregation(self, object_id, from_timestamp, to_timestamp, aggregation, with_comments=False, headers=True): """ Generic method to call the aggregations implemented in the API """ params = { "ids": object_id, "from": from_timestamp, "to": to_timestamp, "withComments": with_comments, "headers": headers } url = output_api_url + self.base_path if aggregation == Aggregation.HOURS.value: url = url + "/hours" elif aggregation == Aggregation.QUARTERS.value: url = url + "/quarters" else: raise ClientException("This aggregation isn't implemented") r = requests.get(url, params, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def put_apikeys(self, writable, expire, description=None): """ Generate a new apikey. An optional description can be specified in the body. PUT /me/apikeys https://bbdata.daplab.ch/api/#me_apikeys_put """ params = {"writable": writable, "expire": expire} data = {"description": description} if len(description) <= 65: raise ValueError("The description is too big") durations = expire.split("-") for duration in durations: d = duration[:-1] u = duration[-1] if d < 0: raise ValueError(d + " duration is lower than 0") if u not in ["d", "h", "m", "s"]: raise ValueError(u + " is not a duration unit") url = output_api_url + self.base_path + "/apikeys" r = requests.put(url, data, params=params, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self): """ GET /info https://bbdata.daplab.ch/api/#info_get """ url = output_api_url + self.base_path r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self, user_group_id): """ Get a user group, including the list of its users. GET /userGroups/{userGroupId} https://bbdata.daplab.ch/api/#usergroups__usergroupid__get """ url = output_api_url + self.base_path + "/" + str(user_group_id) r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self): """ Get the list of all users registered. GET /users https://bbdata.dsaplab.ch/api/#users_get """ url = output_api_url + self.base_path r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def post(self, name, symbol, unit_type): """ POST /units https://bbdata.daplab.ch/api/#units_post """ # TODO Finish the implementation and test it data = {"name": name, "symbol": symbol, "type": unit_type} url = output_api_url + self.base_path r = requests.post(url, data, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self, group_id): """ Get an object group with all its objects. GET /objectGroups/{groupId} https://bbdata.daplab.ch/api/#objectgroups__groupid__get """ url = output_api_url + self.base_path + "/" + str(group_id) r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self, comment_id): """ Get one comment. GET /comments/{id} https://bbdata.daplab.ch/api/#comments__id__get """ url = output_api_url + self.base_path + "/" + str(comment_id) r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_permissions(self, group_id): """ Get the list of user group which have access to the group GET /objectGroups/{groupId}/permissions https://bbdata.daplab.ch/api/#objectgroups__groupid__objects_put """ url = output_api_url + self.base_path + "/" + str( group_id) + "/permissions" r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_objects(self, group_id): """ Get the list of objects part of the group. GET /objectGroups/{groupId}/objects https://bbdata.daplab.ch/api/#objectgroups__groupid__objects_get """ url = output_api_url + self.base_path + "/" + str( group_id) + "/objects" r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_all(self, object_ids=None): """ Get all comments from objects you have read access to. GET /comments https://bbdata.daplab.ch/api/#comments_get """ params = {"objectIds": object_ids} url = output_api_url + self.base_path r = requests.get(url, params, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get(self, object_id): """ Get an object. GET /objects/{objectIs} https://bbdata.daplab.ch/api/#objects__objectid__get """ url = output_api_url + self.base_path + "/" + str(object_id) r = requests.get(url, headers=self.auth.headers) # return ObjectResponse(r.json()) return handle_response(r.status_code, r.json())
def get_groups(self, admin=False): """ Get the list of groups I am part of. GET /me/groups https://bbdata.daplab.ch/api/#me_groups_get """ params = {"admin": admin} url = output_api_url + self.base_path + "/groups" r = requests.get(url, params, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def post_disable(self, object_id): """ Disable this object. All associated tokens will be removed. POST /objects/{objectId}/disable https://bbdata.daplab.ch/api/#objects__objectid__disable_post """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/disable" r = requests.post(url, headers=self.auth.headers) return handle_response(r.status_code, True)
def post_enable(self, object_id): """ Enable this object. POST /objects/{objectId}/enable https://bbdata.daplab.ch/api/#objects__objectid__enable_post """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/enable" r = requests.post(url, headers=self.auth.headers) return handle_response(r.status_code, True)
def delete(self, object_id): """ Delete the object with the given id POST /objects/{objectId} https://bbdata.daplab.ch/api/#objects__objectid__delete """ # TODO This method is in the Postman profile but isn't in the docs url = output_api_url + self.base_path + "/" + str(object_id) r = requests.delete(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_comments(self, object_id): """ Get all comments attached to this object. Use the /comments endpoint for more actions. GET /objects/{objectId}/comments https://bbdata.daplab.ch/api/#objects__objectid__comments_get """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/comments" r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def post(self, object_id, data): """ Edit the name and/or the description of the object. Only the properties appearing in the body will be modified. POST /objects/{objectId} https://bbdata.daplab.ch/api/#objects__objectid__post """ # TODO The data to send isn't define in the API Docs url = output_api_url + self.base_path + "/" + str(object_id) r = requests.post(url, data, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_users(self, user_group_id): """ Get the list of users part of this group, with their access level (administrator or regular user). GET /userGroups/{userGroupId}/users https://bbdata.daplab.ch/api/#usergroups__usergroupid__users_get """ url = output_api_url + self.base_path + "/" + str(user_group_id) \ + "/users" r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_apikeys(self): """ Get the list of apikeys. Apikeys are used to access this api (see the bbtoken used in the security schemes). Apikeys can be read-only or read/write and can have an expiration date. Use read-only apikeys for applications using the api. GET /me/apikeys https://bbdata.daplab.ch/api/#me_apikeys_get """ url = output_api_url + self.base_path + "/apikeys" r = requests.get(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def put_tokens(self, object_id): """ Generate a new secured token. PUT /objects/{objectId}/tokens https://bbdata.daplab.ch/api/#objects__objectid__tokens_put """ # TODO The optional description should probably be added in this # method url = output_api_url + self.base_path + "/" + str(object_id) \ + "/tokens" r = requests.put(url, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def get_all(self, with_objects=False, writable=False): """ Get the list of object groups accessible (in read-only) by the user. GET /objectGroups https://bbdata.daplab.ch/api/#objectgroups_get """ params = { "withObjects": with_objects, "writable": writable, } url = output_api_url + self.base_path r = requests.get(url, params, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def put_object(self, group_id, object_id): """ Add an object to the group. GET /objectGroups/{groupId}/objects https://bbdata.daplab.ch/api/#objectgroups__groupid__objects_put """ data = { "objectId": str(object_id), } url = output_api_url + self.base_path + "/" + str( group_id) + "/objects" r = requests.put(url, data, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def delete_object(self, group_id, object_id): """ Delete the object. DELETE /objectGroups/{groupId}/objects https://bbdata.daplab.ch/api/#objectgroups__groupid__delete """ data = { "objectId": str(object_id), } url = output_api_url + self.base_path + "/" + str( group_id) + "/objects" r = requests.delete(url, data, headers=self.auth.headers) return handle_response(r.status_code, True)
def delete_permissions(self, group_id, group_id_to_delete): """ Revoke a permission. DELETE /objectGroups/{groupId}/permissions https://bbdata.daplab.ch/api/#objectgroups__groupid__objects_delete """ data = { "groupId": str(group_id_to_delete), } url = output_api_url + self.base_path + "/" + str( group_id) + "/permissions" r = requests.delete(url, data, headers=self.auth.headers) return handle_response(r.status_code, True)
def delete_tokens(self, object_id, token_id): """ Revoke a token. DELETE /objects/{objectId}/tokens https://bbdata.daplab.ch/api/#objects__objectid__tokens_delete """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/tokens" params = { "tokenId": token_id } r = requests.delete(url, params=params, headers=self.auth.headers) return handle_response(r.status_code, True)
def post_tokens(self, object_id, description): """ Edit the token's description. POST /objects/{objectId}/tokens https://bbdata.daplab.ch/api/#objects__objectid__tokens_post """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/tokens" json = { "description": description } r = requests.post(url, json=json, headers=self.auth.headers) return handle_response(r.status_code, r.json())
def delete_tags(self, object_id, tags): """ Remove tags. DELETE /objects/{objectId}/tags https://bbdata.daplab.ch/api/#objects__objectid__tags_delete """ url = output_api_url + self.base_path + "/" + str(object_id) \ + "/tags" params = { "tags": tags } r = requests.put(url, params=params, headers=self.auth.headers) return handle_response(r.status_code, True)
def put_permissions(self, group_id, group_id_to_add): """ Grant permissions on the group to a user group. PUT /objectGroups/{groupId}/permissions https://bbdata.daplab.ch/api/#objectgroups__groupid__objects_put """ data = { "groupId": str(group_id_to_add), } url = output_api_url + self.base_path + "/" + str( group_id) + "/permissions" r = requests.put(url, data, headers=self.auth.headers) return handle_response(r.status_code, r.json())