Skip to content

Commit

Permalink
added authentication token to headers of requests in Blueapi RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ZohebShaikh committed Oct 2, 2024
1 parent 7fc224a commit 671b390
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/blueapi/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import TypeAdapter

from blueapi.config import RestConfig
from blueapi.service.authentication import Authentication, AuthenticationType
from blueapi.service.model import (
DeviceModel,
DeviceResponse,
Expand Down Expand Up @@ -127,10 +128,16 @@ def _request_and_deserialize(
get_exception: Callable[[requests.Response], Exception | None] = _exception,
) -> T:
url = self._url(suffix)
auth = Authentication(AuthenticationType.DEVICE)
access_token = auth.load_token()["access_token"]
headers = {
"content-type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {access_token}",
}
if data:
response = requests.request(method, url, json=data)
response = requests.request(method, url, json=data, headers=headers)
else:
response = requests.request(method, url)
response = requests.request(method, url, headers=headers)
exception = get_exception(response)
if exception is not None:
raise exception
Expand Down
3 changes: 2 additions & 1 deletion src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ def load_token(self):
with open("token.txt", "rb") as token_file:
token = token_file.read()
token = cipher_suite.decrypt(token).decode()
# This print is just for testing purposes
# TODO: This print is just for testing purposes
print(token)
return json.loads(token)

def start_device_flow(self):
response = requests.post(
Expand Down

0 comments on commit 671b390

Please sign in to comment.