Skip to content

Commit 8614ff8

Browse files
author
Josie Fegan
authored
Make the OpsRamp api token available as a property (#145)
Make it possible for the caller to get direct access to the OpsRamp API token because we have encountered a few use cases in the field where this is needed and people have been using unsupported hacks to get to it. Provide an approved method to counter this.
1 parent dd8ba91 commit 8614ff8

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

opsramp/binding.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# binding.py
66
# Defines the primary entry points for callers of this library.
77
#
8-
# (c) Copyright 2019-2022 Hewlett Packard Enterprise Development LP
8+
# (c) Copyright 2019-2025 Hewlett Packard Enterprise Development LP
99
#
1010
# Licensed under the Apache License, Version 2.0 (the "License");
1111
# you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ def connect(url, key, secret):
4444

4545

4646
class Opsramp(ORapi):
47-
def __init__(self, url, token):
47+
def __init__(self, url: str, token: str):
4848
self.auth = {
4949
"Authorization": "Bearer " + token,
5050
"Accept": "application/json,application/xml",
@@ -55,6 +55,16 @@ def __init__(self, url, token):
5555
def __str__(self):
5656
return "%s %s" % (str(type(self)), self.api)
5757

58+
@property
59+
def token(self) -> str:
60+
hdr: str = self.auth.get("Authorization", "")
61+
return hdr.partition(" ")[-1]
62+
63+
@token.setter
64+
def token(self, newtok: str) -> str:
65+
self.auth.update({"Authorization": f"Bearer {newtok}"})
66+
return newtok
67+
5868
def config(self):
5969
return GlobalConfig(self)
6070

tests/test_binding.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# (c) Copyright 2019-2022 Hewlett Packard Enterprise Development LP
3+
# (c) Copyright 2019-2025 Hewlett Packard Enterprise Development LP
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@ def setUp(self):
4242
assert adapter.last_request.text == expected_send
4343
assert type(self.ormp) is opsramp.binding.Opsramp
4444
assert self.ormp.auth == expected_auth
45+
assert self.ormp.token == token
4546

4647
def test_str(self):
4748
assert "Opsramp" in str(self.ormp)
@@ -53,3 +54,13 @@ def test_config(self):
5354
def test_tenant(self):
5455
obj = self.ormp.tenant("unit-test")
5556
assert "Tenant" in str(obj)
57+
58+
def test_token_property(self):
59+
tok: str = self.ormp.token
60+
assert tok
61+
newtok: str = "horry patter"
62+
assert tok != newtok
63+
# now change it
64+
self.ormp.token = newtok
65+
assert self.ormp.token == newtok
66+
assert self.ormp.auth["Authorization"] == "Bearer " + newtok

0 commit comments

Comments
 (0)