Skip to content

Commit

Permalink
Merge pull request #105 from UpCloudLtd/modifiable-user-agent
Browse files Browse the repository at this point in the history
API: user agent to class variable
  • Loading branch information
ajmyyra authored May 3, 2021
2 parents 610b7a1 + bad54d9 commit fc1279e
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions upcloud_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import requests

from upcloud_api import __version__
from upcloud_api.errors import UpCloudAPIError


Expand All @@ -11,6 +12,7 @@ class API:
"""

api_root = 'https://api.upcloud.com/1.3'
user_agent = f'upcloud-python-api/{__version__}'

def __init__(self, token, timeout=None):
"""
Expand All @@ -29,7 +31,7 @@ def api_request(self, method, endpoint, body=None, params=None, timeout=-1):
raise Exception('Invalid/Forbidden HTTP method')

url = f'{self.api_root}{endpoint}'
headers = {'Authorization': self.token, 'User-Agent': self._get_user_agent()}
headers = {'Authorization': self.token, 'User-Agent': self.user_agent}

if body:
data = json.dumps(body)
Expand Down Expand Up @@ -91,8 +93,3 @@ def __error_middleware(self, res, res_json):
)

return res_json

def _get_user_agent(self) -> str:
from upcloud_api import __version__

return f'upcloud-python-api/{__version__}'

0 comments on commit fc1279e

Please sign in to comment.