Skip to content

Commit

Permalink
feat: send_post_request(), send_get_request()
Browse files Browse the repository at this point in the history
to send requests to custom endpoints
  • Loading branch information
roslovets committed Sep 16, 2023
1 parent a7decdb commit 2b9e168
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions strapi_client/strapi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,33 @@ async def upsert_entry(
data=data
)

async def send_post_request(
self,
route: str,
body: Optional[dict] = None
) -> dict:
"""Send POST request to custom endpoint."""
route = route.lstrip('/')
url: str = f'{self.baseurl}api/{route}'
async with aiohttp.ClientSession() as session:
async with session.post(url, json=body, headers=self._get_auth_header()) as res:
if res.status != 200:
raise Exception(f'Unable to send POST request, error {res.status}: {res.reason}')
return await res.json()

async def send_get_request(
self,
route: str
) -> dict:
"""Send GET request to custom endpoint."""
route = route.lstrip('/')
url: str = f'{self.baseurl}api/{route}'
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self._get_auth_header()) as res:
if res.status != 200:
raise Exception(f'Unable to send GET request, error {res.status}: {res.reason}')
return await res.json()

def _get_auth_header(self) -> Optional[dict]:
"""Compose auth header from token."""
if self._token:
Expand Down
16 changes: 16 additions & 0 deletions strapi_client/strapi_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ def upsert_entry(
args = locals()
del args['self']
return asyncio.run(self._strapi_client.upsert_entry(**args))

def send_post_request(
self,
route: str,
body: Optional[dict] = None
) -> dict:
"""Send POST request to custom endpoint."""
return asyncio.run(self._strapi_client.send_post_request(route=route, body=body))


def send_get_request(
self,
route: str
) -> dict:
"""Send GET request to custom endpoint."""
return asyncio.run(self._strapi_client.send_get_request(route=route))

0 comments on commit 2b9e168

Please sign in to comment.