Skip to content

Commit

Permalink
Complete options for binary in/out and auth on/off
Browse files Browse the repository at this point in the history
Fixes AB#10061
Fixes AB#10064

Signed-off-by: Jon Geater <[email protected]>
  • Loading branch information
Jon Geater committed Oct 21, 2024
1 parent e533cdf commit fdb7077
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
1 change: 1 addition & 0 deletions archivist/appidp.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ def token(self, client_id: str, client_secret: str) -> AppIDP:
"client_secret": client_secret,
},
data=True,
auth=False,
)
)
56 changes: 42 additions & 14 deletions archivist/archivist.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,14 @@ def __copy__(self) -> "Archivist":
arch._user_agent = self._user_agent
return arch

def _add_headers(self, headers: "dict[str,str]|None") -> "dict[str,Any]":
def _add_headers(self, headers: "dict[str,str]|None", add_auth: "bool" = True) -> "dict[str,Any]":
newheaders = super()._add_headers(headers)

auth = self.auth # this may trigger a refetch so only do it once here
# for appidp endpoint there may not be an authtoken
if auth is not None:
newheaders["authorization"] = "Bearer " + auth.strip()
if add_auth:
auth = self.auth # this may trigger a refetch so only do it once here
# for appidp endpoint there may not be an authtoken
if auth is not None:
newheaders["authorization"] = "Bearer " + auth.strip()

return newheaders

Expand All @@ -244,7 +245,9 @@ def post(
*,
headers: "dict[str,Any]|None" = None,
data: "dict[str, Any] | bool" = False,
) -> "dict[str, Any]":
response_data: "bool" = False,
auth: "bool" = True,
) -> "dict[str, Any] | bytes":
"""POST method (REST)
Creates an entity
Expand All @@ -254,6 +257,8 @@ def post(
request (dict): request body defining the entity
headers (dict): optional REST headers
data (bool): send as form-encoded and not as json
response_data (bool): get response as raw bytes, not JSON
auth (bool): automatically add auth headers
Returns:
dict representing the response body (entity).
Expand All @@ -262,18 +267,21 @@ def post(
response = self.session.post(
url,
data=request,
headers=self._add_headers(headers, auth),
)
else:
response = self.session.post(
url,
json=request,
headers=self._add_headers(headers),
headers=self._add_headers(headers, auth),
)

error = _parse_response(response)
if error is not None:
raise error

if response_data:
return response.content
return response.json()

@retry_429
Expand All @@ -285,7 +293,9 @@ def post_file(
*,
form: str = "file",
params: "dict[str, Any]|None" = None,
) -> "dict[str, Any]":
response_data: "bool" = False,
auth: "bool" = True,
) -> "dict[str, Any] | bytes":
"""POST method (REST) - upload binary
Uploads a file to an endpoint
Expand All @@ -295,6 +305,8 @@ def post_file(
fd : iterable representing the contents of a file.
mtype (str): mime type e.g. image/jpg
params (dict): dictionary of optional path params
response_data (bool): get response as raw bytes, not JSON
auth (bool): automatically add auth headers
Returns:
dict representing the response body (entity).
Expand All @@ -311,7 +323,7 @@ def post_file(
response = self.session.post(
url,
data=multipart, # pyright: ignore https://github.com/requests/toolbelt/issues/312
headers=self._add_headers(headers),
headers=self._add_headers(headers, auth),
params=_dotdict(params),
)

Expand All @@ -321,26 +333,34 @@ def post_file(
if error is not None:
raise error

if response_data:
return response.content
return response.json()

@retry_429
def delete(
self, url: str, *, headers: "dict[str, Any]|None" = None
) -> "dict[str, Any]":
self, url: str,
*,
headers: "dict[str, Any]|None" = None,
response_data: "bool" = False,
auth: "bool" = True,
) -> "dict[str, Any] | bytes":
"""DELETE method (REST)
Deletes an entity
Args:
url (str): e.g. v2/assets/xxxxxxxxxxxxxxxxxxxxxxxxxxxx`
headers (dict): optional REST headers
response_data (bool): get response as raw bytes, not JSON
auth (bool): automatically add auth headers
Returns:
dict representing the response body (entity).
"""
response = self.session.delete(
url,
headers=self._add_headers(headers),
headers=self._add_headers(headers, auth),
)

self._response_ring_buffer.appendleft(response)
Expand All @@ -349,6 +369,8 @@ def delete(
if error is not None:
raise error

if response_data:
return response.content
return response.json()

@retry_429
Expand All @@ -358,7 +380,9 @@ def patch(
request: "dict[str, Any]",
*,
headers: "dict[str, Any]| None" = None,
) -> "dict[str, Any]":
response_data: "bool" = False,
auth: "bool" = True
) -> "dict[str, Any] | bytes":
"""PATCH method (REST)
Updates the specified entity.
Expand All @@ -367,6 +391,8 @@ def patch(
url (str): e.g. v2/assets/xxxxxxxxxxxxxxxxxxxxxxxxxxxx`
request (dict): request body defining the entity changes.
headers (dict): optional REST headers
response_data (bool): get response as raw bytes, not JSON
auth (bool): automatically add auth headers
Returns:
dict representing the response body (entity).
Expand All @@ -375,7 +401,7 @@ def patch(
response = self.session.patch(
url,
json=request,
headers=self._add_headers(headers),
headers=self._add_headers(headers, auth),
)

self._response_ring_buffer.appendleft(response)
Expand All @@ -384,4 +410,6 @@ def patch(
if error is not None:
raise error

if response_data:
return response.content
return response.json()
5 changes: 4 additions & 1 deletion archivist/archivistpublic.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ def get(
*,
headers: "dict[str, str]|None" = None,
params: "dict[str, Any]|None" = None,
) -> "dict[str, Any]":
return_raw: "bool" = False,
) -> "dict[str, Any] | bytes":
"""GET method (REST)
Args:
Expand All @@ -242,6 +243,8 @@ def get(
if error is not None:
raise error

if return_raw:
return response.content
return response.json()

@retry_429
Expand Down

0 comments on commit fdb7077

Please sign in to comment.