-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a new Ideal basic API class.
- Loading branch information
1 parent
d6037d0
commit 5ed5a78
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class BaseAPI(object): | ||
""" | ||
Basic api class for | ||
""" | ||
self.token = "" | ||
|
||
def __init__(self, arg): | ||
super(BaseAPI, self).__init__() | ||
|
||
def __perform_get(self, url, headers=dict(), params=dict()): | ||
return requests.get(url, headers=headers, params=params) | ||
|
||
def __perform_post(self, url, headers=dict(), params=dict()): | ||
headers['content-type'] = 'application/json' | ||
return requests.post(url, headers=headers, params=params) | ||
|
||
def __perform_put(self, url, headers=dict(), params=dict()): | ||
headers['content-type'] = 'application/json' | ||
return requests.put(url, headers=headers, params=params) | ||
|
||
def __perform_delete(self, url, headers=dict(), params=dict()): | ||
headers['content-type'] = 'application/x-www-form-urlencoded' | ||
return requests.delete(url, headers=headers, params=params) | ||
|
||
def __perform_request(self, url, params=dict()): | ||
""" | ||
This method will perform the real request, | ||
in this way we can customize only the "output" of the API call by | ||
using self.__call_api method. | ||
This method will return the request object. | ||
""" | ||
headers = {'Authorization':'Bearer ' + self.token} | ||
if type == 'POST': | ||
r = self.__perform_request(url, headers=headers, params=params) | ||
elif type == 'PUT': | ||
r = self.__perform_put(url, headers=headers, params=params) | ||
elif type == 'DELETE': | ||
r = self.__perform_delete(url, headers=headers, params=params) | ||
else: | ||
r = self.__perform_get(url, headers=headers, params=params) | ||
return r | ||
|
||
def call_api(self, *args, **kargs): | ||
""" | ||
exposes any api entry | ||
useful when working with new API calls that are not yet implemented by Droplet class | ||
""" | ||
return self.__call_api(*args, **kargs) |