Skip to content

Commit

Permalink
Merge branch 'release-0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
romerojunior committed Nov 7, 2017
2 parents c952001 + 6fed3a0 commit ee91063
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cloudianapi.egg-info
dist
.DS_Store
.idea
*.iml
Expand Down
30 changes: 18 additions & 12 deletions cloudianapi/core/basecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __call__(self, **parameters):
:type parameters: dict
:rtype: BaseComponent
"""
api_call = self._build_request(**parameters)
request = self._build_request(**parameters)

return self.requestor.http_get(api_call)
return self.requestor.request(**request)

def __getattr__(self, endpoint):
# closure, expects keyword argument unpacking:
Expand All @@ -64,9 +64,9 @@ def _inner_getattr(self, endpoint, **parameters):
:type parameters: dict
:rtype: BaseComponent
"""
api_call = self._build_request(endpoint, **parameters)
request = self._build_request(endpoint, **parameters)

return self.requestor.http_get(api_call)
return self.requestor.request(**request)

def _build_request(self, endpoint='', **parameters):
""" Builds a proper API request.
Expand All @@ -75,24 +75,30 @@ def _build_request(self, endpoint='', **parameters):
:type endpoint: str
:param parameters: URL query parameters
:type parameters: dict
:rtype: str
:rtype: dict
"""

base_url = self.__class__.base_url
request = {
'method': parameters.pop('method', 'GET'),
'data': parameters.pop('data', None),
'json': parameters.pop('json', None)
}

if endpoint:
api_call = '{base_url}/{endpoint}'.format(
base_url=base_url,
url = '{base_url}/{endpoint}'.format(
base_url=self.__class__.base_url,
endpoint=endpoint
)
else:
api_call = '{base_url}'.format(
base_url=base_url,
url = '{base_url}'.format(
base_url=self.__class__.base_url,
)

for index, (key, value) in enumerate(parameters.items()):
api_call += '{symbol}{key}={value}'.format(
url += '{symbol}{key}={value}'.format(
symbol='&' if index else '?', key=key, value=value
)

return api_call
request['url'] = url

return request
28 changes: 20 additions & 8 deletions cloudianapi/core/requestors.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,38 @@ def __init__(self, url, port, user, key, warn=False):
self.key = key
self.warn = warn

def http_get(self, call_str):
def request(self, url, data=None, json=None, method='GET'):
""" Builds a request and fetches its response from the admin API
server, returns a JSON encoded string.
server, returns a JSON encoded string. SSL verification is disabled.
Simply put: this method works as a request.requests() wrapper.
:param call_str: the endpoint with all parameters to GET request
:type call_str: str
:param url: the endpoint with all parameters to GET request
:type url: str
:param method: HTTP method (GET, POST, HEAD, DELETE, etc)
:type method: str
:param data: file-like object to send in the body of the request
:type data: dict
:param json: json data to send in the body of the request
:type json: dict
:rtype: str
"""
if not self.warn:
requests.urllib3.disable_warnings()

api_call = '{url}:{port}/{call}'.format(
url=self.url, port=self.port, call=call_str
url=self.url, port=self.port, call=url
)

try:
response = requests.get(
api_call, verify=False, auth=(
response = requests.request(
verify=False,
method=method,
url=api_call,
data=data,
json=json,
auth=(
self.user, self.key
)
),
)
if response.status_code == 200:
try:
Expand Down
14 changes: 13 additions & 1 deletion examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
print client.system.license()

# Print details about a given group:
print client.group(groupId='ABC')
print client.group(groupId="ABC")

# Print a list of active users from a given user group:
print client.user.list(groupId="ABC", userType="all", userStatus="active")
Expand All @@ -64,3 +64,15 @@
value=client.monitor.host(nodeId=node)['diskUsedKb']['value'],
node=node
)

# Deleting user Cartman from a given group:
print client.user(method="DELETE", userId="Cartman", groupId="ABC")

# Adding user Butters to a given group:
payload = {
"userId": "Butters",
"groupId": "ABC",
"userType": "User"
}

print client.user(method='PUT', json=payload)
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@

# Romero Galiza Jr. - [email protected]

from distutils.core import setup
from setuptools import find_packages
from setuptools import setup, find_packages

base_url = 'https://github.com/romerojunior/cloudian-api'
version_tag = '0.1.0'
version_tag = '0.2.0'

setup(
name='cloudianapi',
version=version_tag,
description='Non-official Cloudian (TM) REST Admin API Client',
long_description=open('README.md').read(),
author='Romero Galiza Jr',
license='LICENSE.txt',
author_email='[email protected]',
url=base_url,
download_url=base_url + '/archive/' + version_tag + '.tar.gz',
Expand Down

0 comments on commit ee91063

Please sign in to comment.