Skip to content

Commit

Permalink
Created a new Ideal basic API class.
Browse files Browse the repository at this point in the history
  • Loading branch information
koalalorenzo committed Aug 11, 2014
1 parent d6037d0 commit 5ed5a78
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions digitalocean/baseapi.py
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)

0 comments on commit 5ed5a78

Please sign in to comment.