Skip to content

Commit

Permalink
[UPD] account_statement_import_online_gocardless: refactor requests m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
ljsalvatierra-factorlibre committed Aug 7, 2024
1 parent 64fd49e commit 7354077
Showing 1 changed file with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from odoo.exceptions import UserError
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT as DF

GOCARDLESS_ENDPOINT = "https://bankaccountdata.gocardless.com/api/v2"
GOCARDLESS_API = "https://bankaccountdata.gocardless.com/api/v2"
REQUESTS_TIMEOUT = 60


Expand Down Expand Up @@ -46,6 +46,44 @@ def _get_available_services(self):
("gocardless", "GoCardless"),
]

def _gocardless_get_headers(self, basic=False):
"""Generic method for providing the needed request headers."""
self.ensure_one()
headers = {

Check warning on line 52 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L51-L52

Added lines #L51 - L52 were not covered by tests
"accept": "application/json",
"Content-Type": "application/json",
}
if not basic:
headers["Authorization"] = f"Bearer {self._gocardless_get_token()}"
return headers

Check warning on line 58 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L57-L58

Added lines #L57 - L58 were not covered by tests

def _gocardless_get_request(self, endpoint, params=None):
content = {}
url = url_join(GOCARDLESS_API, endpoint)
response = requests.get(

Check warning on line 63 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L61-L63

Added lines #L61 - L63 were not covered by tests
url,
params=params,
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
)
if response.status_code == 200:
content = json.loads(response.text)
return response, content

Check warning on line 71 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L70-L71

Added lines #L70 - L71 were not covered by tests

def _gocardless_post_request(self, endpoint, data=None):
content = {}
url = url_join(GOCARDLESS_API, endpoint)
response = requests.post(

Check warning on line 76 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L74-L76

Added lines #L74 - L76 were not covered by tests
url,
data=data,
headers=self._gocardless_get_headers(basic=True),
timeout=REQUESTS_TIMEOUT,
)

if response.status_code in [200, 201]:
content = json.loads(response.text)
return response, content

Check warning on line 85 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L84-L85

Added lines #L84 - L85 were not covered by tests

def _gocardless_get_token(self):
"""Resolve and return the corresponding GoCardless token for doing the requests.
If there's still no token, it's requested. If it exists, but it's expired and
Expand All @@ -59,20 +97,15 @@ def _gocardless_get_token(self):
self.gocardless_refresh_token
and now > self.gocardless_refresh_expiration
):
url = f"{GOCARDLESS_ENDPOINT}/token/refresh/"
endpoint = "token/refresh"

Check warning on line 100 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L100

Added line #L100 was not covered by tests
else:
url = f"{GOCARDLESS_ENDPOINT}/token/new/"
response = requests.post(
url,
endpoint = "token/new"
_response, data = self._gocardless_post_request(

Check warning on line 103 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L102-L103

Added lines #L102 - L103 were not covered by tests
endpoint,
data=json.dumps(
{"secret_id": self.username, "secret_key": self.password}
),
headers=self._gocardless_get_headers(basic=True),
timeout=REQUESTS_TIMEOUT,
)
data = {}
if response.status_code == 200:
data = json.loads(response.text)
expiration_date = now + relativedelta(seconds=data.get("access_expires", 0))
vals = {
"gocardless_token": data.get("access", False),
Expand All @@ -86,17 +119,6 @@ def _gocardless_get_token(self):
self.sudo().write(vals)
return self.gocardless_token

def _gocardless_get_headers(self, basic=False):
"""Generic method for providing the needed request headers."""
self.ensure_one()
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
if not basic:
headers["Authorization"] = f"Bearer {self._gocardless_get_token()}"
return headers

def action_select_gocardless_bank(self):
if not self.journal_id.bank_account_id:
raise UserError(
Expand Down Expand Up @@ -137,15 +159,12 @@ def _gocardless_select_bank_instituion(self):
country = (
self.journal_id.bank_account_id.company_id or self.journal_id.company_id
).country_id
response = requests.get(
f"{GOCARDLESS_ENDPOINT}/institutions/",
params={"country": country.code},
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
response, data = self._gocardless_get_request(

Check warning on line 162 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L162

Added line #L162 was not covered by tests
"institutions", params={"country": country.code}
)
if response.status_code == 400:
raise UserError(_("Incorrect country code or country not supported."))
institutions = json.loads(response.text)
institutions = data

Check warning on line 167 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L167

Added line #L167 was not covered by tests
# Prepare data for being showed in the JS widget
ctx = self.env.context.copy()
ctx.update(
Expand All @@ -172,53 +191,37 @@ def action_check_gocardless_agreement(self):
self.gocardless_requisition_ref = str(uuid4())
base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url")
redirect_url = url_join(base_url, "gocardless/response")
response = requests.post(
f"{GOCARDLESS_ENDPOINT}/requisitions/",
_response, data = self._gocardless_post_request(

Check warning on line 194 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L194

Added line #L194 was not covered by tests
"requisitions/",
data=json.dumps(
{
"redirect": redirect_url,
"institution_id": self.gocardless_institution_id,
"reference": self.gocardless_requisition_ref,
}
),
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
)
if response.status_code == 201:
requisition_data = json.loads(response.text)
if data:
requisition_data = data

Check warning on line 205 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L205

Added line #L205 was not covered by tests
self.gocardless_requisition_id = requisition_data["id"]
# JS code expects here to return a plain link or nothing
return requisition_data["link"]

def _gocardless_request_requisition(self):
response = requests.get(
f"{GOCARDLESS_ENDPOINT}/requisitions/{self.gocardless_requisition_id}/",
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
_response, data = self._gocardless_get_request(

Check warning on line 211 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L211

Added line #L211 was not covered by tests
f"requisitions/{self.gocardless_requisition_id}"
)
if response.status_code == 200:
return json.loads(response.text)
return {}
return data

Check warning on line 214 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L214

Added line #L214 was not covered by tests

def _gocardless_request_account(self, account_id):
response = requests.get(
f"{GOCARDLESS_ENDPOINT}/accounts/{account_id}/",
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
)
if response.status_code == 200:
return json.loads(response.text)
return {}
_response, data = self._gocardless_get_request(f"accounts/{account_id}")
return data

Check warning on line 218 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L217-L218

Added lines #L217 - L218 were not covered by tests

def _gocardless_request_agreement(self, agreement_id):
response = requests.get(
f"{GOCARDLESS_ENDPOINT}/agreements/enduser/" f"{agreement_id}/",
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
_response, data = self._gocardless_get_request(

Check warning on line 221 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L221

Added line #L221 was not covered by tests
f"agreements/enduser/{agreement_id}"
)
if response.status_code == 200:
return json.loads(response.text)
return {}
return data

Check warning on line 224 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L224

Added line #L224 was not covered by tests

def _gocardless_finish_requisition(self, dry=False):
"""Once the requisiton to the bank institution has been made, and this is called
Expand Down Expand Up @@ -295,19 +298,14 @@ def _gocardless_request_transactions(self, date_since, date_until):
now = fields.Datetime.now()
if now > date_since and now < date_until:
date_until = now
transaction_response = requests.get(
f"{GOCARDLESS_ENDPOINT}/accounts/"
f"{self.gocardless_account_id}/transactions/",
_response, data = self._gocardless_get_request(

Check warning on line 301 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L301

Added line #L301 was not covered by tests
f"accounts/{self.gocardless_account_id}/transactions",
params={
"date_from": date_since.strftime(DF),
"date_to": date_until.strftime(DF),
},
headers=self._gocardless_get_headers(),
timeout=REQUESTS_TIMEOUT,
)
if transaction_response.status_code == 200:
return json.loads(transaction_response.text)
return {}
return data

Check warning on line 308 in account_statement_import_online_gocardless/models/online_bank_statement_provider.py

View check run for this annotation

Codecov / codecov/patch

account_statement_import_online_gocardless/models/online_bank_statement_provider.py#L308

Added line #L308 was not covered by tests

def _gocardless_obtain_statement_data(self, date_since, date_until):
"""Called from the cron or the manual pull wizard to obtain transactions for
Expand Down

0 comments on commit 7354077

Please sign in to comment.