Skip to content

Commit

Permalink
chore: Add retry for 429 error
Browse files Browse the repository at this point in the history
  • Loading branch information
pprzetacznik committed Oct 27, 2024
1 parent 1889932 commit 2ab021b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3
1.4
70 changes: 40 additions & 30 deletions ksef_utils/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from json import dumps
from base64 import b64encode
from hashlib import sha256
import requests
from requests import Session
from requests.adapters import HTTPAdapter, Retry
from ksef_utils.utils import (
render_template,
sign_xml,
Expand All @@ -18,11 +19,21 @@ def __init__(self, config):
self.config = config
if config.LOGS_VERBOSE:
debug_requests()
retries = Retry(
total=10,
backoff_factor=5,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=frozenset(
{"DELETE", "GET", "HEAD", "OPTIONS", "PUT", "TRACE", "POST"}
),
)
self.session = Session()
self.session.mount(self.config.URL, HTTPAdapter(max_retries=retries))

def init_token(self, **kwargs):
rendered_template = render_template("InitSession.xml", **kwargs)
print(rendered_template)
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Session/InitToken",
data=rendered_template,
headers={
Expand All @@ -33,7 +44,7 @@ def init_token(self, **kwargs):
return response

def init_signed(self, data):
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Session/InitSigned",
data=data,
headers={
Expand All @@ -49,7 +60,7 @@ def get_status(self, session_token):
"SessionToken": session_token,
"Content-Type": "application/json",
}
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/online/Session/Status?PageSize=10&PageOffset=0&IncludeDetails=true",
headers=headers,
)
Expand All @@ -59,7 +70,7 @@ def get_upo(self, session_token, reference_number):
headers = {
"accept": "application/json",
}
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/common/Status/{reference_number}",
headers=headers,
)
Expand All @@ -72,7 +83,7 @@ def authorization_challenge(self, identifier, identifier_type="onip"):
"identifier": identifier,
}
}
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Session/AuthorisationChallenge?PageSize=10&PageOffset=0&IncludeDetails=false",
json=data,
)
Expand All @@ -81,6 +92,7 @@ def authorization_challenge(self, identifier, identifier_type="onip"):
print(dir(response))
print(response.text)
print(response.reason)
print(response.status_code)
response_json = response.json()
return response_json

Expand All @@ -90,7 +102,7 @@ def get_session_terminate(self, session_token):
"SessionToken": session_token,
"Content-Type": "application/json",
}
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/online/Session/Terminate",
headers=headers,
)
Expand Down Expand Up @@ -123,7 +135,7 @@ def get_invoices(self, session_token, from_date=None, to_date=None):
"SessionToken": session_token,
"Content-Type": "application/json",
}
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Query/Invoice/Sync?PageSize=10&PageOffset=0",
json=data,
headers=headers,
Expand Down Expand Up @@ -162,7 +174,7 @@ def generate_token(self, session_token):
"description": "0_ksef-utils_test_token",
}
}
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Credentials/GenerateToken",
json=data,
headers={
Expand All @@ -185,9 +197,11 @@ def post_context_grant(
"grantContextCredentials": {
"contextIdentifier": {
"type": context_identifier_type,
"identifier": context_identifier
if context_identifier
else self.config.KSEF_NIP,
"identifier": (
context_identifier
if context_identifier
else self.config.KSEF_NIP
),
},
"credentialsIdentifier": {
"type": credentials_identifier_type,
Expand All @@ -200,7 +214,7 @@ def post_context_grant(
},
}
}
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Credentials/ContextGrant",
json=data,
headers={
Expand Down Expand Up @@ -259,7 +273,7 @@ def post_credentials_grant(self, session_token, onip):
],
}
}
response = requests.post(
response = self.session.post(
f"{self.config.URL}/api/online/Credentials/Grant",
json=data,
headers={
Expand All @@ -278,7 +292,7 @@ def get_credentials_grant(self, session_token):
# "targetIdentifier": "",
# }
# }
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/online/Query/Credential/Context/Sync",
headers={
"Content-Type": "application/json",
Expand All @@ -291,7 +305,7 @@ def get_credentials_grant(self, session_token):
def get_generate_internal_identifier(
self, session_token, input_digits_sequence
):
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/online/Session/GenerateInternalIdentifier/{input_digits_sequence}",
headers={
"Content-Type": "application/json",
Expand All @@ -302,7 +316,7 @@ def get_generate_internal_identifier(
return response

def get_token_status(self, session_token, reference_number):
response = requests.get(
response = self.session.get(
f"{self.config.URL}/api/online/Credentials/Status/{reference_number}",
headers={
"Content-Type": "application/json",
Expand All @@ -319,7 +333,7 @@ def get_invoice_status(self, reference_number: str, session_token: str):
"Content-Type": "application/json",
}
url = f"{self.config.URL}/api/online/Invoice/Status/{reference_number}"
response = requests.get(url, headers=headers)
response = self.session.get(url, headers=headers)
return response

def get_invoice(self, reference_number: str, session_token: str):
Expand All @@ -328,7 +342,7 @@ def get_invoice(self, reference_number: str, session_token: str):
"SessionToken": session_token,
}
url = f"{self.config.URL}/api/online/Invoice/Get/{reference_number}"
response = requests.get(url, headers=headers)
response = self.session.get(url, headers=headers)
return response

def send_invoice(self, data: dict, session_token: str):
Expand All @@ -338,7 +352,7 @@ def send_invoice(self, data: dict, session_token: str):
"SessionToken": session_token,
"Content-Type": "application/json",
}
response = requests.put(url, data=dumps(data), headers=headers)
response = self.session.put(url, data=dumps(data), headers=headers)
return response

def post_payment_identifier(
Expand All @@ -351,7 +365,7 @@ def post_payment_identifier(
"Content-Type": "application/json",
}
url = f"{self.config.URL}/api/online/Payment/Identifier/Request"
response = requests.post(url, json=data, headers=headers)
response = self.session.post(url, json=data, headers=headers)
return response


Expand Down Expand Up @@ -422,7 +436,7 @@ def wait_until_logged(self):
print(dumps(response_json, indent=4))
if response_json.get("processingCode") == 315:
logged = True
sleep(1)
sleep(2)
return response_json

def send_invoice(self, **kwargs):
Expand Down Expand Up @@ -456,13 +470,11 @@ def wait_until_invoice(self, reference_number):
if not invoice_status.get("ksefReferenceNumber"):
invoice_status = {}
if not invoice_status:
sleep(1)
sleep(2)
return response.json()

def get_invoice_status(self, reference_number: str):
return self.server.get_invoice_status(
reference_number, self.init_token
)
return self.server.get_invoice_status(reference_number, self.init_token)

def get_invoice(self, reference_number: str) -> str:
response = self.server.get_invoice(reference_number, self.init_token)
Expand Down Expand Up @@ -501,7 +513,7 @@ def wait_until_token(self, element_reference_number):
processing_code = response_status.json().get("processingCode")
print(dumps(response.json(), indent=4))
if processing_code != 200:
sleep(1)
sleep(2)
return response.json()

def post_payment_identifier(
Expand All @@ -513,9 +525,7 @@ def post_payment_identifier(
return response.json()

def get_invoices(self, from_date=None, to_date=None):
response = self.server.get_invoices(
self.init_token, from_date, to_date
)
response = self.server.get_invoices(self.init_token, from_date, to_date)
return response.json()

def get_status(self):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ classifiers =
Intended Audience :: Customer Service
License :: Other/Proprietary License
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
license_files =
LICENSE

Expand Down

0 comments on commit 2ab021b

Please sign in to comment.