api_provider.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from http import HTTPStatus
  2. from typing import Callable
  3. from urllib.parse import urlencode
  4. from urllib.parse import urljoin
  5. from pydantic import AnyHttpUrl
  6. from urllib3 import PoolManager
  7. from urllib3 import Retry
  8. from clean_python import ctx
  9. from clean_python import Json
  10. from .exceptions import ApiException
  11. __all__ = ["SyncApiProvider"]
  12. def is_success(status: HTTPStatus) -> bool:
  13. """Returns True on 2xx status"""
  14. return (int(status) // 100) == 2
  15. def join(url: str, path: str) -> str:
  16. """Results in a full url without trailing slash"""
  17. assert url.endswith("/")
  18. assert not path.startswith("/")
  19. result = urljoin(url, path)
  20. if result.endswith("/"):
  21. result = result[:-1]
  22. return result
  23. def add_query_params(url: str, params: Json | None) -> str:
  24. if params is None:
  25. return url
  26. return url + "?" + urlencode(params, doseq=True)
  27. class SyncApiProvider:
  28. """Basic JSON API provider with retry policy and bearer tokens.
  29. The default retry policy has 3 retries with 1, 2, 4 second intervals.
  30. Args:
  31. url: The url of the API (with trailing slash)
  32. fetch_token: Callable that returns a token for a tenant id
  33. retries: Total number of retries per request
  34. backoff_factor: Multiplier for retry delay times (1, 2, 4, ...)
  35. """
  36. def __init__(
  37. self,
  38. url: AnyHttpUrl,
  39. fetch_token: Callable[[PoolManager, int], str | None],
  40. retries: int = 3,
  41. backoff_factor: float = 1.0,
  42. ):
  43. self._url = str(url)
  44. assert self._url.endswith("/")
  45. self._fetch_token = fetch_token
  46. self._pool = PoolManager(retries=Retry(retries, backoff_factor=backoff_factor))
  47. def request(
  48. self,
  49. method: str,
  50. path: str,
  51. params: Json | None = None,
  52. json: Json | None = None,
  53. fields: Json | None = None,
  54. timeout: float = 5.0,
  55. ) -> Json | None:
  56. assert ctx.tenant is not None
  57. url = join(self._url, path)
  58. token = self._fetch_token(self._pool, ctx.tenant.id)
  59. headers = {}
  60. if token is not None:
  61. headers["Authorization"] = f"Bearer {token}"
  62. response = self._pool.request(
  63. method=method,
  64. url=add_query_params(url, params),
  65. json=json,
  66. fields=fields,
  67. headers=headers,
  68. timeout=timeout,
  69. )
  70. status = HTTPStatus(response.status)
  71. content_type = response.headers.get("Content-Type")
  72. if content_type is None and status is HTTPStatus.NO_CONTENT:
  73. return {"status": int(status)} # we have to return something...
  74. if content_type != "application/json":
  75. raise ApiException(
  76. f"Unexpected content type '{content_type}'", status=status
  77. )
  78. body = response.json()
  79. if status is HTTPStatus.NOT_FOUND:
  80. return None
  81. elif is_success(status):
  82. return body
  83. else:
  84. raise ApiException(body, status=status)