|
| 1 | +# This file was auto-generated by Fern from our API Definition. |
| 2 | + |
| 3 | +from ..core.client_wrapper import SyncClientWrapper |
| 4 | +import typing |
| 5 | +from .types.streaks_rankings_request_type import StreaksRankingsRequestType |
| 6 | +from ..core.request_options import RequestOptions |
| 7 | +from ..types.streak_ranking_user import StreakRankingUser |
| 8 | +from ..core.pydantic_utilities import parse_obj_as |
| 9 | +from ..errors.unauthorized_error import UnauthorizedError |
| 10 | +from ..types.error_body import ErrorBody |
| 11 | +from ..errors.unprocessable_entity_error import UnprocessableEntityError |
| 12 | +from json.decoder import JSONDecodeError |
| 13 | +from ..core.api_error import ApiError |
| 14 | +from ..core.client_wrapper import AsyncClientWrapper |
| 15 | + |
| 16 | + |
| 17 | +class StreaksClient: |
| 18 | + def __init__(self, *, client_wrapper: SyncClientWrapper): |
| 19 | + self._client_wrapper = client_wrapper |
| 20 | + |
| 21 | + def rankings( |
| 22 | + self, |
| 23 | + *, |
| 24 | + limit: typing.Optional[int] = None, |
| 25 | + type: typing.Optional[StreaksRankingsRequestType] = None, |
| 26 | + request_options: typing.Optional[RequestOptions] = None, |
| 27 | + ) -> typing.List[StreakRankingUser]: |
| 28 | + """ |
| 29 | + Get the top users by streak length (active or longest). |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + limit : typing.Optional[int] |
| 34 | + Number of users to return. Must be between 1 and 100. |
| 35 | +
|
| 36 | + type : typing.Optional[StreaksRankingsRequestType] |
| 37 | + Whether to rank users by active streaks or longest streaks ever achieved. |
| 38 | +
|
| 39 | + request_options : typing.Optional[RequestOptions] |
| 40 | + Request-specific configuration. |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + typing.List[StreakRankingUser] |
| 45 | + Successful operation |
| 46 | +
|
| 47 | + Examples |
| 48 | + -------- |
| 49 | + from trophy import TrophyApi |
| 50 | +
|
| 51 | + client = TrophyApi( |
| 52 | + api_key="YOUR_API_KEY", |
| 53 | + ) |
| 54 | + client.streaks.rankings() |
| 55 | + """ |
| 56 | + _response = self._client_wrapper.httpx_client.request( |
| 57 | + "streaks/rankings", |
| 58 | + method="GET", |
| 59 | + params={ |
| 60 | + "limit": limit, |
| 61 | + "type": type, |
| 62 | + }, |
| 63 | + request_options=request_options, |
| 64 | + ) |
| 65 | + try: |
| 66 | + if 200 <= _response.status_code < 300: |
| 67 | + return typing.cast( |
| 68 | + typing.List[StreakRankingUser], |
| 69 | + parse_obj_as( |
| 70 | + type_=typing.List[StreakRankingUser], # type: ignore |
| 71 | + object_=_response.json(), |
| 72 | + ), |
| 73 | + ) |
| 74 | + if _response.status_code == 401: |
| 75 | + raise UnauthorizedError( |
| 76 | + typing.cast( |
| 77 | + ErrorBody, |
| 78 | + parse_obj_as( |
| 79 | + type_=ErrorBody, # type: ignore |
| 80 | + object_=_response.json(), |
| 81 | + ), |
| 82 | + ) |
| 83 | + ) |
| 84 | + if _response.status_code == 422: |
| 85 | + raise UnprocessableEntityError( |
| 86 | + typing.cast( |
| 87 | + ErrorBody, |
| 88 | + parse_obj_as( |
| 89 | + type_=ErrorBody, # type: ignore |
| 90 | + object_=_response.json(), |
| 91 | + ), |
| 92 | + ) |
| 93 | + ) |
| 94 | + _response_json = _response.json() |
| 95 | + except JSONDecodeError: |
| 96 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 97 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
| 98 | + |
| 99 | + |
| 100 | +class AsyncStreaksClient: |
| 101 | + def __init__(self, *, client_wrapper: AsyncClientWrapper): |
| 102 | + self._client_wrapper = client_wrapper |
| 103 | + |
| 104 | + async def rankings( |
| 105 | + self, |
| 106 | + *, |
| 107 | + limit: typing.Optional[int] = None, |
| 108 | + type: typing.Optional[StreaksRankingsRequestType] = None, |
| 109 | + request_options: typing.Optional[RequestOptions] = None, |
| 110 | + ) -> typing.List[StreakRankingUser]: |
| 111 | + """ |
| 112 | + Get the top users by streak length (active or longest). |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + limit : typing.Optional[int] |
| 117 | + Number of users to return. Must be between 1 and 100. |
| 118 | +
|
| 119 | + type : typing.Optional[StreaksRankingsRequestType] |
| 120 | + Whether to rank users by active streaks or longest streaks ever achieved. |
| 121 | +
|
| 122 | + request_options : typing.Optional[RequestOptions] |
| 123 | + Request-specific configuration. |
| 124 | +
|
| 125 | + Returns |
| 126 | + ------- |
| 127 | + typing.List[StreakRankingUser] |
| 128 | + Successful operation |
| 129 | +
|
| 130 | + Examples |
| 131 | + -------- |
| 132 | + import asyncio |
| 133 | +
|
| 134 | + from trophy import AsyncTrophyApi |
| 135 | +
|
| 136 | + client = AsyncTrophyApi( |
| 137 | + api_key="YOUR_API_KEY", |
| 138 | + ) |
| 139 | +
|
| 140 | +
|
| 141 | + async def main() -> None: |
| 142 | + await client.streaks.rankings() |
| 143 | +
|
| 144 | +
|
| 145 | + asyncio.run(main()) |
| 146 | + """ |
| 147 | + _response = await self._client_wrapper.httpx_client.request( |
| 148 | + "streaks/rankings", |
| 149 | + method="GET", |
| 150 | + params={ |
| 151 | + "limit": limit, |
| 152 | + "type": type, |
| 153 | + }, |
| 154 | + request_options=request_options, |
| 155 | + ) |
| 156 | + try: |
| 157 | + if 200 <= _response.status_code < 300: |
| 158 | + return typing.cast( |
| 159 | + typing.List[StreakRankingUser], |
| 160 | + parse_obj_as( |
| 161 | + type_=typing.List[StreakRankingUser], # type: ignore |
| 162 | + object_=_response.json(), |
| 163 | + ), |
| 164 | + ) |
| 165 | + if _response.status_code == 401: |
| 166 | + raise UnauthorizedError( |
| 167 | + typing.cast( |
| 168 | + ErrorBody, |
| 169 | + parse_obj_as( |
| 170 | + type_=ErrorBody, # type: ignore |
| 171 | + object_=_response.json(), |
| 172 | + ), |
| 173 | + ) |
| 174 | + ) |
| 175 | + if _response.status_code == 422: |
| 176 | + raise UnprocessableEntityError( |
| 177 | + typing.cast( |
| 178 | + ErrorBody, |
| 179 | + parse_obj_as( |
| 180 | + type_=ErrorBody, # type: ignore |
| 181 | + object_=_response.json(), |
| 182 | + ), |
| 183 | + ) |
| 184 | + ) |
| 185 | + _response_json = _response.json() |
| 186 | + except JSONDecodeError: |
| 187 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 188 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
0 commit comments