Skip to content

Commit

Permalink
Accumulate user_agent (#268)
Browse files Browse the repository at this point in the history
* Accumulate user_agent

User Agent is a property which can be prepended to by other
packages such as datatrails-samples so that DataTrails-User-Agent
is a list of agents.

datatrails-samples will have its own copy of the Archivist class
that will add its own user-agent. Likewise for avid-samples-service.

AB#9572
  • Loading branch information
eccles authored Jul 11, 2024
1 parent 692b016 commit 14e6b38
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 34 deletions.
12 changes: 7 additions & 5 deletions archivist/archivist.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ def __init__(
verify: bool = True,
max_time: float = MAX_TIME,
partner_id: str = "",
user_agent: str = "",
):
super().__init__(
fixtures=fixtures,
verify=verify,
max_time=max_time,
partner_id=partner_id,
user_agent=user_agent,
)

if isinstance(auth, tuple):
Expand Down Expand Up @@ -205,22 +203,26 @@ def auth(self) -> "str | None":
@property
def Public(self) -> ArchivistPublic: # pylint: disable=invalid-name
"""Get a Public instance"""
return ArchivistPublic(
arch = ArchivistPublic(
fixtures=deepcopy(self._fixtures),
verify=self._verify,
max_time=self._max_time,
partner_id=self._partner_id,
)
arch._user_agent = self._user_agent # pylint: disable=protected-access
return arch

def __copy__(self) -> "Archivist":
return Archivist(
arch = Archivist(
self._url,
self.auth,
fixtures=deepcopy(self._fixtures),
verify=self._verify,
max_time=self._max_time,
partner_id=self._partner_id,
user_agent=self._user_agent,
)
arch._user_agent = self._user_agent
return arch

def _add_headers(self, headers: "dict[str,str]|None") -> "dict[str,Any]":
newheaders = super()._add_headers(headers)
Expand Down
24 changes: 12 additions & 12 deletions archivist/archivistpublic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ def __init__(
verify: bool = True,
max_time: float = MAX_TIME,
partner_id: str = "",
user_agent: str = "",
):
self._verify = verify
self._response_ring_buffer = deque(maxlen=self.RING_BUFFER_MAX_LEN)
self._session = None
self._max_time = max_time
self._fixtures = fixtures or {}
self._partner_id = partner_id
self._user_agent = user_agent
self._user_agent = f"{USER_AGENT_PREFIX}{self.version}"

# Type hints for IDE autocomplete, keep in sync with CLIENTS map above
self.assets: _AssetsPublic
Expand Down Expand Up @@ -171,9 +170,14 @@ def partner_id(self) -> str:

@property
def user_agent(self) -> str:
"""str: Returns partner id if set when initialising an instance of this class"""
"""str: Returns user agent"""
return self._user_agent

@user_agent.setter
def user_agent(self, value):
"""str: Prepends user agent to user_agent property"""
self._user_agent = f"{value} {self.user_agent}"

@property
def fixtures(self) -> "dict[str, Any]":
"""dict: Contains predefined attributes for each endpoint"""
Expand All @@ -185,22 +189,18 @@ def fixtures(self, fixtures: "dict[str, Any]"):
self._fixtures = _deepmerge(self._fixtures, fixtures)

def __copy__(self):
return ArchivistPublic(
arch = ArchivistPublic(
fixtures=deepcopy(self._fixtures),
verify=self._verify,
max_time=self._max_time,
partner_id=self.partner_id,
)
arch._user_agent = self._user_agent
return arch

def _add_headers(self, headers: "dict[str, str]|None") -> "dict[str, str]":
newheaders = {**headers} if headers is not None else {}
u = self.user_agent
if u:
newheaders[USER_AGENT] = (
f"{self.user_agent} "
f"{USER_AGENT_PREFIX}{self.version}"
)
else:
newheaders[USER_AGENT] = f"{USER_AGENT_PREFIX}{self.version}"
newheaders[USER_AGENT] = self.user_agent

p = self.partner_id
if p:
Expand Down
4 changes: 2 additions & 2 deletions functests/execaccess_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

# these are for access_policies
self.ac_props = deepcopy(PROPS)
Expand Down Expand Up @@ -311,8 +311,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth_2,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
# creates reciprocal subjects for arch 1 and arch 2.
# subject 1 contains details of subject 2 to be shared
self.subject_1, self.subject_2 = self.arch.subjects.share(
Expand Down
2 changes: 1 addition & 1 deletion functests/execapplications.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.display_name = f"{DISPLAY_NAME} {uuid4()}"

def tearDown(self):
Expand Down
4 changes: 2 additions & 2 deletions functests/execassets.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def setUp(self):
auth,
max_time=30,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.attrs = deepcopy(ATTRS)
self.traffic_light = deepcopy(ATTRS)
self.traffic_light["arc_display_type"] = "Traffic light with violation camera"
Expand Down Expand Up @@ -248,8 +248,8 @@ def setUp(self):
auth,
max_time=30,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

def tearDown(self):
self.arch.close()
Expand Down
4 changes: 2 additions & 2 deletions functests/execattachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.file_uuid: str = ""

with suppress(FileNotFoundError):
Expand Down Expand Up @@ -221,8 +221,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

def tearDown(self):
self.arch.close()
Expand Down
2 changes: 1 addition & 1 deletion functests/execcompliance_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.identities = []

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion functests/execnotebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def setUp(self):
self.url,
(self.client_id, self.client_secret),
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

def tearDown(self):
self.arch.close()
Expand Down
2 changes: 1 addition & 1 deletion functests/execpublicassets.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def setUp(self):
auth,
max_time=30,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.attrs = deepcopy(ATTRS)
self.traffic_light = deepcopy(ATTRS)
self.traffic_light["arc_display_type"] = "Traffic light with violation camera"
Expand Down
2 changes: 1 addition & 1 deletion functests/execrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def setUp(self):
auth,
max_time=30,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

def tearDown(self):
self.arch.close()
Expand Down
2 changes: 1 addition & 1 deletion functests/execsubjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def setUp(self):
getenv("DATATRAILS_URL"),
auth,
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE
self.display_name = f"{DISPLAY_NAME} {uuid4()}"

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ valid-metaclass-classmethod-first-arg = ["cls"]
# ignored-parents =

# Maximum number of arguments for function / method.
max-args = 8
max-args = 7

# Maximum number of attributes for a class (see R0902).
max-attributes = 7
Expand Down
2 changes: 1 addition & 1 deletion unittests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""

PARTNER_ID_VALUE = "acmecorp"
USER_AGENT_VALUE = "archivist-samples/v0.1.0"
USER_AGENT_VALUE = "samples/v0.1.0"
5 changes: 2 additions & 3 deletions unittests/testaccess_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def setUp(self):
"url",
"authauthauth",
partner_id=PARTNER_ID_VALUE,
user_agent=USER_AGENT_VALUE,
)
self.arch.user_agent = USER_AGENT_VALUE

def tearDown(self):
self.arch.close()
Expand Down Expand Up @@ -130,8 +130,7 @@ def test_access_policies_create(self):
"headers": {
"authorization": "Bearer authauthauth",
USER_AGENT: (
f"{USER_AGENT_VALUE} "
f"{USER_AGENT_PREFIX}{VERSION}"
f"{USER_AGENT_VALUE} " f"{USER_AGENT_PREFIX}{VERSION}"
),
PARTNER_ID: PARTNER_ID_VALUE,
},
Expand Down

0 comments on commit 14e6b38

Please sign in to comment.