From 2e866d54bb498f9441aace66d601d0e14676063c Mon Sep 17 00:00:00 2001 From: Hugo SCHOCH Date: Wed, 29 Jun 2016 14:37:32 +0200 Subject: [PATCH] Add API structure to code --- srcs/BLIH.py | 140 ++++++++++++++++++++++++++++++++++++++++++++-- srcs/BLIHAPI.json | 132 ------------------------------------------- 2 files changed, 136 insertions(+), 136 deletions(-) delete mode 100644 srcs/BLIHAPI.json diff --git a/srcs/BLIH.py b/srcs/BLIH.py index 9ff39aa..781a166 100644 --- a/srcs/BLIH.py +++ b/srcs/BLIH.py @@ -3,16 +3,146 @@ import hashlib import request +_routes = { + "repository": + [ + { + "action": "create", + "route": "/repositories", + "arguments": None, + "method": "POST", + "data": + [ + { + "name": "name", + "default": None, + "optional": False + }, + { + "name": "type", + "default": "git", + "optional": False + }, + { + "name": "description", + "default": None, + "optional": True + } + ] + }, + { + "action": "list", + "route": "/repositories", + "arguments": None, + "method": "GET", + "data": None + }, + { + "action": "delete", + "route": "/repository/$", + "arguments": + [ + "name" + ], + "method": "DELETE", + "data": None + }, + { + "action": "info", + "route": "/repository/$", + "arguments": + [ + "name" + ], + "method": "GET", + "data": None + }, + { + "action": "getacl", + "route": "/repository/$/acls", + "arguments": + [ + "name" + ], + "method": "GET", + "data": None + }, + { + "action": "setacl", + "route": "/repository/$/acls", + "arguments": + [ + "name" + ], + "method": "POST", + "data": + [ + { + "name": "user", + "default": None, + "optional": False + }, + { + "name": "acl", + "default": None, + "optional": False + } + ] + } + ], + "sshkey": + [ + { + "action": "add", + "route": "/sshkeys", + "arguments": None, + "method": "POST", + "data": + [ + { + "name": "sshkey", + "default": None, + "optional": False + } + ] + }, + { + "action": "delete", + "route": "/sshkey/$", + "arguments": + [ + "sshkey" + ], + "method": "DELETE", + "data": None + }, + { + "action": "list", + "route": "/sshkeys", + "arguments": None, + "method": "GET", + "data": None + } + ], + "whoami": + [ + { + "action": "whoami", + "route": "/whoami", + "arguments": None, + "method": "GET", + "data": None + } + ] +} + class API(object): - def __init__(self, login, token, baseURL="https://blih.epitech.eu", routes="srcs/BLIHAPI.json"): + def __init__(self, login, token, baseURL="https://blih.epitech.eu"): super(API, self).__init__() self._base = baseURL self._login = login self._token = bytearray(str(token), 'utf8') - if routes: - with open(routes) as data: - self._actions = json.load(data) def make_body(self, data=None): hash_signature = hmac.new(self._token, msg=bytearray(self._login, 'utf-8'), digestmod=hashlib.sha512) @@ -96,6 +226,7 @@ class Repository(API): def __init__(self, login, token, baseURL="https://blih.epitech.eu"): super(Repository, self).__init__(login, token, baseURL, None) + self._actions = _routes["repository"] def create(self, args): data = {"name": args[0], "type": "git", "description": ""} @@ -121,6 +252,7 @@ class SSHKey(API): def __init__(self, login, token, baseURL="https://blih.epitech.eu"): super(SSHKey, self).__init__(login, token, baseURL, None) + self._actions = _routes["sshkey"] def upload(self, args): with open(args[0], "r") as key: diff --git a/srcs/BLIHAPI.json b/srcs/BLIHAPI.json deleted file mode 100644 index 770ad58..0000000 --- a/srcs/BLIHAPI.json +++ /dev/null @@ -1,132 +0,0 @@ -{ - "repository": - [ - { - "action": "create", - "route": "/repositories", - "arguments": null, - "method": "POST", - "data": - [ - { - "name": "name", - "default": null, - "optional": false - }, - { - "name": "type", - "default": "git", - "optional": false - }, - { - "name": "description", - "default": null, - "optional": true - } - ] - }, - { - "action": "list", - "route": "/repositories", - "arguments": null, - "method": "GET", - "data": null - }, - { - "action": "delete", - "route": "/repository/$", - "arguments": - [ - "name" - ], - "method": "DELETE", - "data": null - }, - { - "action": "info", - "route": "/repository/$", - "arguments": - [ - "name" - ], - "method": "GET", - "data": null - }, - { - "action": "getacl", - "route": "/repository/$/acls", - "arguments": - [ - "name" - ], - "method": "GET", - "data": null - }, - { - "action": "setacl", - "route": "/repository/$/acls", - "arguments": - [ - "name" - ], - "method": "POST", - "data": - [ - { - "name": "user", - "default": null, - "optional": false - }, - { - "name": "acl", - "default": null, - "optional": false - } - ] - } - ], - "sshkey": - [ - { - "action": "add", - "route": "/sshkeys", - "arguments": null, - "method": "POST", - "data": - [ - { - "name": "sshkey", - "default": null, - "optional": false - } - ] - }, - { - "action": "delete", - "route": "/sshkey/$", - "arguments": - [ - "sshkey" - ], - "method": "DELETE", - "data": null - }, - { - "action": "list", - "route": "/sshkeys", - "arguments": null, - "method": "GET", - "data": null - } - ], - "whoami": - [ - { - "action": "whoami", - "route": "/whoami", - "arguments": null, - "method": "GET", - "data": null - } - ] -} \ No newline at end of file