Skip to content

Commit

Permalink
Merge pull request #37 from gopythongo/timeout
Browse files Browse the repository at this point in the history
Timeout
  • Loading branch information
jdelic authored Jan 14, 2019
2 parents b98747f + 8f14aa8 commit 9de1dba
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions aptly_api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ def __init__(self, *args: Any, status_code: int = 0) -> None:

class BaseAPIClient:
def __init__(self, base_url: str, ssl_verify: Union[str, bool, None] = None,
ssl_cert: Optional[Tuple[str, str]] = None, http_auth: Optional[AuthBase] = None) -> None:
ssl_cert: Optional[Tuple[str, str]] = None, http_auth: Optional[AuthBase] = None,
timeout: int = 60) -> None:
self.base_url = base_url
self.ssl_verify = ssl_verify
self.ssl_cert = ssl_cert
self.http_auth = http_auth
self.exc_class = AptlyAPIException
self.timeout = timeout

def _error_from_response(self, resp: requests.Response) -> str:
if resp.status_code == 200:
Expand Down Expand Up @@ -54,7 +56,7 @@ def _make_url(self, path: str) -> str:

def do_get(self, urlpath: str, params: Dict[str, str] = None) -> requests.Response:
resp = requests.get(self._make_url(urlpath), params=params, verify=self.ssl_verify,
cert=self.ssl_cert, auth=self.http_auth)
cert=self.ssl_cert, auth=self.http_auth, timeout=self.timeout)

if resp.status_code < 200 or resp.status_code >= 300:
raise AptlyAPIException(self._error_from_response(resp), status_code=resp.status_code)
Expand All @@ -72,7 +74,8 @@ def do_post(self, urlpath: str, data: Union[bytes, MutableMapping[str, str], IO[
] = None,
json: MutableMapping[Any, Any] = None) -> requests.Response:
resp = requests.post(self._make_url(urlpath), data=data, params=params, files=files, json=json,
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth)
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth,
timeout=self.timeout)

if resp.status_code < 200 or resp.status_code >= 300:
raise AptlyAPIException(self._error_from_response(resp), status_code=resp.status_code)
Expand All @@ -89,7 +92,8 @@ def do_put(self, urlpath: str, data: Union[bytes, MutableMapping[str, str], IO[A
] = None,
json: MutableMapping[Any, Any] = None) -> requests.Response:
resp = requests.put(self._make_url(urlpath), data=data, files=files, json=json,
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth)
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth,
timeout=self.timeout)

if resp.status_code < 200 or resp.status_code >= 300:
raise AptlyAPIException(self._error_from_response(resp), status_code=resp.status_code)
Expand All @@ -100,7 +104,8 @@ def do_delete(self, urlpath: str, params: Dict[str, str] = None,
data: Union[str, Dict[str, str], Sequence[Tuple[str, str]]] = None,
json: Union[List[Dict[str, Any]], Dict[str, Any]] = None) -> requests.Response:
resp = requests.delete(self._make_url(urlpath), params=params, data=data, json=json,
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth)
verify=self.ssl_verify, cert=self.ssl_cert, auth=self.http_auth,
timeout=self.timeout)

if resp.status_code < 200 or resp.status_code >= 300:
raise AptlyAPIException(self._error_from_response(resp), status_code=resp.status_code)
Expand Down

0 comments on commit 9de1dba

Please sign in to comment.