Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions opsramp/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# binding.py
# Defines the primary entry points for callers of this library.
#
# (c) Copyright 2019-2022 Hewlett Packard Enterprise Development LP
# (c) Copyright 2019-2025 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,7 +44,7 @@ def connect(url, key, secret):


class Opsramp(ORapi):
def __init__(self, url, token):
def __init__(self, url: str, token: str):
self.auth = {
"Authorization": "Bearer " + token,
"Accept": "application/json,application/xml",
Expand All @@ -55,6 +55,16 @@ def __init__(self, url, token):
def __str__(self):
return "%s %s" % (str(type(self)), self.api)

@property
def token(self) -> str:
hdr: str = self.auth.get("Authorization", "")
return hdr.partition(" ")[-1]

@token.setter
def token(self, newtok: str) -> str:
self.auth.update({"Authorization": f"Bearer {newtok}"})
return newtok

def config(self):
return GlobalConfig(self)

Expand Down
13 changes: 12 additions & 1 deletion tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# (c) Copyright 2019-2022 Hewlett Packard Enterprise Development LP
# (c) Copyright 2019-2025 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,7 @@ def setUp(self):
assert adapter.last_request.text == expected_send
assert type(self.ormp) is opsramp.binding.Opsramp
assert self.ormp.auth == expected_auth
assert self.ormp.token == token

def test_str(self):
assert "Opsramp" in str(self.ormp)
Expand All @@ -53,3 +54,13 @@ def test_config(self):
def test_tenant(self):
obj = self.ormp.tenant("unit-test")
assert "Tenant" in str(obj)

def test_token_property(self):
tok: str = self.ormp.token
assert tok
newtok: str = "horry patter"
assert tok != newtok
# now change it
self.ormp.token = newtok
assert self.ormp.token == newtok
assert self.ormp.auth["Authorization"] == "Bearer " + newtok