Skip to content

Commit

Permalink
fix: add retry_after value from header to ManyRequestsResponse error
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Jan 31, 2023
1 parent 2c2bff7 commit 25dc803
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions .swagger-codegen-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
git_push.sh
tox.ini
test/*.py
fingerprint_pro_server_api_sdk/models/many_requests_response.py
10 changes: 5 additions & 5 deletions fingerprint_pro_server_api_sdk/api/fingerprint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

# python 2 and python 3 compatibility library
import six
from fingerprint_pro_server_api_sdk import rest

from fingerprint_pro_server_api_sdk.api_client import ApiClient
from fingerprint_pro_server_api_sdk.extend_exception import extend_exception
from fingerprint_pro_server_api_sdk.rest import ApiException


Expand Down Expand Up @@ -133,10 +133,10 @@ def get_event_with_http_info(self, request_id, **kwargs): # noqa: E501
except ApiException as e:
if e.status == 403:
error = self.api_client.deserialize(e, 'ErrorEvent403Response', True)
raise rest.KnownApiException(e, error)
raise extend_exception(e, error)
if e.status == 404:
error = self.api_client.deserialize(e, 'ErrorEvent404Response', True)
raise rest.KnownApiException(e, error)
raise extend_exception(e, error)
raise e

def get_visits(self, visitor_id, **kwargs): # noqa: E501
Expand Down Expand Up @@ -254,8 +254,8 @@ def get_visits_with_http_info(self, visitor_id, **kwargs): # noqa: E501
except ApiException as e:
if e.status == 403:
error = self.api_client.deserialize(e, 'ErrorVisits403', True)
raise rest.KnownApiException(e, error)
raise extend_exception(e, error)
if e.status == 429:
error = self.api_client.deserialize(e, 'ManyRequestsResponse', True)
raise rest.KnownApiException(e, error)
raise extend_exception(e, error)
raise e
7 changes: 7 additions & 0 deletions fingerprint_pro_server_api_sdk/extend_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fingerprint_pro_server_api_sdk.rest import KnownApiException


def extend_exception(exception, error_object):
if exception.status == 429:
error_object.retry_after = int(exception.headers.get('retry-after'))
return KnownApiException(exception, error_object)
16 changes: 14 additions & 2 deletions fingerprint_pro_server_api_sdk/models/many_requests_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import six


class ManyRequestsResponse(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand All @@ -28,18 +29,21 @@ class ManyRequestsResponse(object):
and the value is json key in definition.
"""
swagger_types = {
'error': 'str'
'error': 'str',
'retry_after': 'int'
}

attribute_map = {
'error': 'error'
'error': 'error',
'retry_after': 'retry_after'
}

def __init__(self, error=None): # noqa: E501
"""ManyRequestsResponse - a model defined in Swagger""" # noqa: E501
self._error = None
self.discriminator = None
self.error = error
self._retry_after = 0

@property
def error(self):
Expand All @@ -66,6 +70,14 @@ def error(self, error):

self._error = error

@property
def retry_after(self):
return self._retry_after

@retry_after.setter
def retry_after(self, retry_after):
self._retry_after = retry_after

def to_dict(self):
"""Returns the model properties as a dict"""
result = {}
Expand Down
5 changes: 4 additions & 1 deletion generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ done
# jar was downloaded from here https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.34/

rm docs/*
rm -rf fingerprint_pro_server_api_sdk/*
cd fingerprint_pro_server_api_sdk/models
shopt -s extglob
rm !("many_requests_response.py")
cd ../..
java -jar ./bin/swagger-codegen-cli.jar generate -t ./template -l python -i ./res/fingerprint-server-api.yaml -o ./ -c config.json -DpackageVersion=$VERSION
4 changes: 2 additions & 2 deletions template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import re # noqa: F401

# python 2 and python 3 compatibility library
import six
from {{packageName}} import rest

from {{packageName}}.api_client import ApiClient
from {{packageName}}.extend_exception import extend_exception
from {{packageName}}.rest import ApiException


Expand Down Expand Up @@ -213,7 +213,7 @@ class {{classname}}(object):
{{^isDefault}}
if e.status == {{code}}:
error = self.api_client.deserialize(e, '{{dataType}}', True)
raise rest.KnownApiException(e, error)
raise extend_exception(e, error)
{{/isDefault}}
{{/responses}}
raise e
Expand Down
2 changes: 1 addition & 1 deletion test/test_fingerprint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def test_get_visits_error_429(self):
with self.assertRaises(KnownApiException) as context:
self.api.get_visits(mock_file)
self.assertEqual(context.exception.status, 429)
self.assertIsNotNone(context.exception.headers.get('retry-after'))
self.assertIsInstance(context.exception.structured_error, ManyRequestsResponse)
self.assertEqual(context.exception.structured_error.retry_after, 4)

def test_get_event_correct_data(self):
"""Test checks correct code run result in default scenario"""
Expand Down

0 comments on commit 25dc803

Please sign in to comment.