mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-15 01:21:48 +03:00
dfdb294c50
adding metadata to api # Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import requests
|
|
from logger import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
def get_api_call_response_as_text(
|
|
method, api_url, params, search_params, secrets
|
|
) -> str:
|
|
headers = {}
|
|
|
|
api_url_with_search_params = api_url
|
|
if search_params:
|
|
api_url_with_search_params += "?"
|
|
for search_param in search_params:
|
|
api_url_with_search_params += (
|
|
f"{search_param}={search_params[search_param]}&"
|
|
)
|
|
|
|
for secret in secrets:
|
|
headers[secret] = secrets[secret]
|
|
|
|
try:
|
|
if method in ["GET", "DELETE"]:
|
|
response = requests.request(
|
|
method,
|
|
url=api_url_with_search_params,
|
|
params=params or None,
|
|
headers=headers or None,
|
|
)
|
|
elif method in ["POST", "PUT", "PATCH"]:
|
|
response = requests.request(
|
|
method,
|
|
url=api_url_with_search_params,
|
|
json=params or None,
|
|
headers=headers or None,
|
|
)
|
|
else:
|
|
raise ValueError(f"Invalid method: {method}")
|
|
|
|
return response.text
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error calling API: {e}")
|
|
return None
|