Skip to content

Commit

Permalink
replace CH class with function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nawaz committed Feb 20, 2024
1 parent 927749f commit 1e598f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Companies House API
COMPANIES_HOUSE_API_KEY = env("COMPANIES_HOUSE_API_KEY", default=None)
COMPANIES_HOUSE_API_KEY = env.str("COMPANIES_HOUSE_API_KEY", default="")

# GOV NOTIFY
GOV_NOTIFY_API_KEY = env.str("GOV_NOTIFY_API_KEY")
Expand Down
34 changes: 20 additions & 14 deletions report_a_breach/core/services/ch_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

import requests
from django.conf import settings
from exceptions import CompaniesHouseException

COMPANIES_HOUSE_BASIC_AUTH = base64.b64encode( # /PS-IGNORE
bytes(f"{settings.COMPANIES_HOUSE_API_KEY}:", "utf-8")
).decode("utf-8")
COMPANIES_HOUSE_BASE_DOMAIN = "https://api.companieshouse.gov.uk"


class CompaniesHouseApi:
def get_details_from_companies_house(self, registration_number):
"""
Retrieves and returns details of a company from Companies House
using registration number that is passed in.
"""
def get_details_from_companies_house(self, registration_number):
"""
Retrieves and returns details of a company from Companies House
using registration number that is passed in.
"""

if registration_number:
headers_get_company = {"Authorization": f"Basic {COMPANIES_HOUSE_BASIC_AUTH}"}
response = requests.get(
f"{COMPANIES_HOUSE_BASE_DOMAIN}/company/{registration_number}",
headers=headers_get_company,
if registration_number:
headers_get_company = {"Authorization": f"Basic {COMPANIES_HOUSE_BASIC_AUTH}"}
response = requests.get(
f"{COMPANIES_HOUSE_BASE_DOMAIN}/company/{registration_number}",
headers=headers_get_company,
)
if response.status_code == 200:
return response.json()
else:
raise CompaniesHouseException(
f"Companies House API request failed: {response.status_code}"
)
if response.status_code == 200:
return response.json()
return {}
else:
raise CompaniesHouseException("No registration number provided for Companies House API")
return {}
4 changes: 4 additions & 0 deletions report_a_breach/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class CompaniesHouseException(Exception):
"""Exception raised when issue encountered with Companies House API"""

pass

0 comments on commit 1e598f3

Please sign in to comment.