Skip to content

Commit

Permalink
Merge pull request #9 from solianadaniel1/master
Browse files Browse the repository at this point in the history
Modified the Endpoints in Crouton_Client
  • Loading branch information
cobycloud authored Nov 7, 2024
2 parents cd73302 + 1527efb commit 80d9242
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 34 deletions.
105 changes: 81 additions & 24 deletions pkgs/crouton_client/crouton_client/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,110 @@
logger = logging.getLogger(__name__)

class AsyncCroutonClient:
def __init__(self, API_ROOT, ACCESS_STRING):
def __init__(self, API_ROOT, ACCESS_STRING = None):
self.API_ROOT = API_ROOT
self.ACCESS_STRING = ACCESS_STRING

async def get(self, resource: str, item_id: str = None):
async def get(self, resource: str, item_id: str = None, filter_key: str = None, filter_value: str = None):
url = self.API_ROOT + resource

# Add the item_id if provided (for specific resource requests)
if item_id:
res = r.get(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING)
if res.status_code == 200:
return res.model_dump_json()
url += f'/{item_id}'

# If item_id is present, just add the access string
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
logger.error(f"GET request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError
pass
else:
res = r.get(self.API_ROOT + resource + self.ACCESS_STRING)
if res.status_code == 200:
return res.model_dump_json()
# If item_id is not present, add the filters and then the access string
query_params = {}

if filter_key and filter_value:
query_params[filter_key] = filter_value

# Construct the query string from the dictionary (filters only)
query_string = "&".join([f"{key}={value}" for key, value in query_params.items()])

# Append the access string with a slash if it exists
if self.ACCESS_STRING:
if query_string:
query_string += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
query_string = f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
logger.error(f"GET request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError
# If no access token, leave the query string as is (just the filters)
pass

# Add the query string to the URL if it's not empty
if query_string:
url += "?" + query_string

# Perform the GET request
res = r.get(url)
if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"GET request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError

async def post(self, resource: str, data_obj: dict):
if 'id' not in data_obj:
data_obj.update({'id': UUIDGenerator().create()})
res = r.post(self.API_ROOT + resource + self.ACCESS_STRING, json=data_obj)

url = self.API_ROOT + resource

# Add the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
pass

res = r.post(url, json=data_obj)

if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"POST request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError

async def put(self, resource: str, data_obj: dict, item_id: str = None):
if item_id:
res = r.put(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING, json=data_obj)
if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"PUT request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError
url = self.API_ROOT + resource

async def delete(self, resource: str, item_id: str = None):
if item_id:
res = r.delete(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING)
url += f'/{item_id}'

# Append the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
res = r.delete(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING)
pass

# Perform the PUT request with the constructed URL
res = r.put(url, json=data_obj)

if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"DELETE request failed with status {res.status_code}: {res.model_dump_json()}")
logger.error(f"PUT request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError

async def delete(self, resource: str, item_id: str = None):
url = self.API_ROOT + resource

if item_id:
url += f'/{item_id}'
# Append the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"

# Perform the DELETE request with the constructed URL
res = r.delete(url)

# Check if the request was successful
if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"DELETE request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError
82 changes: 72 additions & 10 deletions pkgs/crouton_client/crouton_client/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,111 @@
logger = logging.getLogger(__name__)

class CroutonClient:
def __init__(self, API_ROOT, ACCESS_STRING):
def __init__(self, API_ROOT, ACCESS_STRING = None):
self.API_ROOT = API_ROOT
self.ACCESS_STRING = ACCESS_STRING

def api_get_call(self, resource: str, item_id: str = None):

def api_get_call(self, resource: str, item_id: str = None, filter_key: str = None, filter_value: str = None):
url = self.API_ROOT + resource

# Add the item_id if provided (for specific resource requests)
if item_id:
res = r.get(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING)
url += f'/{item_id}'

# If item_id is present, just add the access string
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
url += ""
else:
res = r.get(self.API_ROOT + resource + self.ACCESS_STRING)
# If item_id is not present, add the filters and then the access string
query_params = {}

if filter_key and filter_value:
query_params[filter_key] = filter_value

# Construct the query string from the dictionary (filters only)
query_string = "&".join([f"{key}={value}" for key, value in query_params.items()])

# Append the access string with a slash if it exists
if self.ACCESS_STRING:
if query_string:
query_string += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
query_string = f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
# If no access token, leave the query string as is (just the filters)
pass

# Add the query string to the URL if it's not empty
if query_string:
url += "?" + query_string

# Perform the GET request
res = r.get(url)
if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"GET request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError(res.status_code)


def api_post_call(self, resource: str, data_obj: dict):
if 'id' not in data_obj:
data_obj.update({'id': UUIDGenerator().create()})

res = r.post(self.API_ROOT + resource + self.ACCESS_STRING, json=data_obj)

url = self.API_ROOT + resource

# Add the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
pass

res = r.post(url, json=data_obj)

if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"POST request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError(res.status_code)


def api_put_call(self, resource: str, data_obj: dict, item_id: str):
res = r.put(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING, json=data_obj)
url = self.API_ROOT + resource

if item_id:
url += f'/{item_id}'

# Append the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"
else:
pass

# Perform the PUT request with the constructed URL
res = r.put(url, json=data_obj)

if res.status_code == 200:
return res.model_dump_json()
else:
logger.error(f"PUT request failed with status {res.status_code}: {res.model_dump_json()}")
raise ValueError(res.status_code)

def api_delete_call(self, resource: str, item_id: str = None):
url = self.API_ROOT + resource

if item_id:
res = r.delete(self.API_ROOT + resource + '/' + item_id + self.ACCESS_STRING)
else:
res = r.delete(self.API_ROOT + resource + self.ACCESS_STRING)
url += f'/{item_id}'
# Append the access string if it's present
if self.ACCESS_STRING:
url += f"/<token={self.ACCESS_STRING.strip('?')}>"

# Perform the DELETE request with the constructed URL
res = r.delete(url)

# Check if the request was successful
if res.status_code == 200:
return res.model_dump_json()
else:
Expand Down

0 comments on commit 80d9242

Please sign in to comment.