From 04a5cb01b0a0f5c089d5f8183302eea0584a0194 Mon Sep 17 00:00:00 2001 From: jmurugan-fzj Date: Fri, 17 Nov 2023 12:13:56 +0100 Subject: [PATCH] - Added logic to set HTTPBasicAuth using api-token for get and post requests. --- src/pyDataverse/api.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/pyDataverse/api.py b/src/pyDataverse/api.py index 22aeefe..bd4d824 100644 --- a/src/pyDataverse/api.py +++ b/src/pyDataverse/api.py @@ -3,6 +3,7 @@ import subprocess as sp from requests import ConnectionError, Response, delete, get, post, put +from requests.auth import HTTPBasicAuth from pyDataverse.exceptions import ( ApiAuthorizationError, @@ -112,13 +113,13 @@ def get_request(self, url, params=None, auth=False): Response object of requests library. """ - params = {} - params["User-Agent"] = "pydataverse" + params = {"User-Agent": "pydataverse"} if self.api_token: - params["key"] = str(self.api_token) - + params["key"] = self.api_token try: - resp = get(url, params=params) + resp = get(url, + params=params, + auth=HTTPBasicAuth(self.api_token, '') if self.api_token else None) if resp.status_code == 401: error_msg = resp.json()["message"] raise ApiAuthorizationError( @@ -171,7 +172,11 @@ def post_request(self, url, data=None, auth=False, params=None, files=None): params["key"] = self.api_token try: - resp = post(url, data=data, params=params, files=files) + resp = post(url, + data=data, + params=params, + files=files, + auth=HTTPBasicAuth(self.api_token, '') if self.api_token else None) if resp.status_code == 401: error_msg = resp.json()["message"] raise ApiAuthorizationError(