Skip to content

Commit

Permalink
add request_api
Browse files Browse the repository at this point in the history
  • Loading branch information
alzheltkovskiy-hubspot committed Oct 24, 2024
1 parent e031d49 commit 2e1a281
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 86 deletions.
6 changes: 6 additions & 0 deletions hubspot/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib3.util.retry import Retry


class Client:
def __init__(
self,
Expand Down Expand Up @@ -86,3 +87,8 @@ def settings(self):
def webhooks(self):
from .discovery.webhooks.discovery import Discovery as WebhooksDiscovery
return WebhooksDiscovery(self.config)

def api_request(self, options):
from .utils.requests.http_request_builder import Request
request = Request(self.config, options)
return request.send()
30 changes: 11 additions & 19 deletions hubspot/utils/requests/http_auth.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
class Auth:
@staticmethod
def get_auth_types() -> list:
return ["accessToken", "developerApiKey", "hapikey"]
return ["access_token", "api_key"]

@staticmethod
def _has_auth_value(source: dict) -> bool:
return bool(source.get("auth_value"))
def _has_auth_value(source: dict, key: str) -> bool:
return bool(source.get(key))

@staticmethod
def choose_auth(options: dict, config: dict) -> dict:
def choose_auth(config: dict, options: dict) -> dict:
auth_types = Auth.get_auth_types()
auth_type = options.get("auth_type")

if auth_type in auth_types and Auth._has_auth_value(options):
return {
"auth_type": auth_type,
"auth_value": options["auth_value"]
}
if auth_type in auth_types and Auth._has_auth_value(config, auth_type):
return {"auth_type": auth_type}

config_auth_type = config.get("auth_type")
if config_auth_type in auth_types and Auth._has_auth_value(config):
return {
"auth_type": config_auth_type,
"auth_value": config["auth_value"]
}
else:
for key in auth_types:
if key in config and Auth._has_auth_value(config, key):
return {"auth_type": key}

return {
"auth_type": None,
"auth_value": None
}
return {"auth_type": None}

14 changes: 5 additions & 9 deletions hubspot/utils/requests/http_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ def get_options_for_sending(self) -> dict:
return options

def apply_auth(self):
auth = Auth.choose_auth(self.options, self.config)
auth = Auth.choose_auth(self.config, self.options)

if auth["auth_type"]:
if auth["auth_type"] == "hapikey":
if auth["auth_type"] == "api_key":
self.options["qs"] = self.options.get("qs", {})
self.options["qs"]["hapikey"] = auth["auth_value"]
self.options["qs"]["hapikey"] = self.config.get(auth["auth_type"])

if auth["auth_type"] == "developerApiKey":
self.options["qs"] = self.options.get("qs", {})
self.options["qs"]["hapikey"] = auth["auth_value"]

if auth["auth_type"] == "accessToken":
self.headers["Authorization"] = f"Bearer {auth['auth_value']}"
if auth["auth_type"] == "access_token":
self.headers["Authorization"] = f"Bearer {self.config.get(auth['auth_type'])}"

def init_headers(self):
if self.default_json:
Expand Down
40 changes: 20 additions & 20 deletions tests/spec/utils/requests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@


@pytest.mark.parametrize(
"source,expected", [
({"auth_value": "test_value"}, True),
({}, False)
"source,key,expected", [
({"auth_value": "test_value"}, "auth_value", True),
({}, "key", False)
]
)
def test_has_auth_value(source, expected):
assert Auth._has_auth_value(source) is expected
def test_has_auth_value(source, key, expected):
assert Auth._has_auth_value(source, key) is expected


@pytest.mark.parametrize(
"options, config, expected", [
"config, options, expected", [
# Valid data in options
({"auth_type": "accessToken", "auth_value": "test_token"},
{"auth_type": "hapikey", "auth_value": "test_apikey"},
{"auth_type": "accessToken", "auth_value": "test_token"}),
({"access_token": "test_api_key"},
{"auth_type": "access_token"},
{"auth_type": "access_token"}),
# Valid data in config, options is invalid
({"auth_type": "invalidType", "auth_value": None},
{"auth_type": "hapikey", "auth_value": "test_apikey"},
{"auth_type": "hapikey", "auth_value": "test_apikey"}),
({"api_key": "test_api_key"},
{"auth_type": "access_token"},
{"auth_type": "api_key"}),
# Invalid data in both options and config
({"auth_type": "invalidType", "auth_value": None},
{"auth_type": "invalidType", "auth_value": None},
{"auth_type": None, "auth_value": None}),
({"invalid_type": None},
{"auth_type": "invalid_type"},
{"auth_type": None}),
# auth_value missing in options, config is also not valid
({"auth_type": "accessToken"},
{"auth_type": "hapikey", "auth_value": None},
{"auth_type": None, "auth_value": None}),
({"api_key": None},
{"auth_type": "access_token"},
{"auth_type": None}),
]
)
def test_choose_auth(options, config, expected):
result = Auth.choose_auth(options, config)
def test_choose_auth(config, options, expected):
result = Auth.choose_auth(config, options)
assert result == expected
47 changes: 9 additions & 38 deletions tests/spec/utils/requests/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
@pytest.fixture
def config():
return {
"user_agent": "custom-python-client/1.0"
"access_token": "test_access_token",
"api_key": "test_api_key"
}


Expand All @@ -37,59 +38,29 @@ def options():
(
{
"path": BASE_PATH,
"auth_type": "developerApiKey",
"auth_value": "test_value_from_options"
"auth_type": "api_key",
},
{
"auth_type": "accessToken",
"auth_value": "test_value_from_config"
"api_key": "test_api_key",
},
f"https://api.hubapi.com{BASE_PATH}?hapikey=test_value_from_options",
f"https://api.hubapi.com{BASE_PATH}?hapikey=test_api_key",
COMMON_HEADERS
),
(
{
"path": BASE_PATH,
"auth_type": "hapikey",
"auth_value": "test_value_from_options"
"auth_type": "access_token",
},
{
"auth_type": "accessToken",
"auth_value": "test_value_from_config"
},
f"https://api.hubapi.com{BASE_PATH}?hapikey=test_value_from_options",
COMMON_HEADERS
),
(
{
"path": BASE_PATH,
"auth_type": "accessToken",
"auth_value": "test_value_from_options"
},
{
"auth_type": "developerApiKey",
"auth_value": "test_value_from_config"
"access_token": "test_access_token",
"api_key": "test_api_key"
},
f"https://api.hubapi.com{BASE_PATH}",
{
**COMMON_HEADERS,
"Authorization": "Bearer test_value_from_options"
"Authorization": "Bearer test_access_token"
}
),
(
{
"path": BASE_PATH,
},
{
"auth_type": "accessToken",
"auth_value": "test_value_from_config"
},
f"https://api.hubapi.com{BASE_PATH}",
{
**COMMON_HEADERS,
"Authorization": "Bearer test_value_from_config"
}
)
]
)
def test_generate_url(options, config, expected_url, expected_headers):
Expand Down

0 comments on commit 2e1a281

Please sign in to comment.