|
| 1 | +# This file was auto-generated by Fern from our API Definition. |
| 2 | + |
| 3 | +import typing |
| 4 | +from ....core.client_wrapper import SyncClientWrapper |
| 5 | +from .types.create_streak_freezes_request_freezes_item import ( |
| 6 | + CreateStreakFreezesRequestFreezesItem, |
| 7 | +) |
| 8 | +from ....core.request_options import RequestOptions |
| 9 | +from ....types.create_streak_freezes_response import CreateStreakFreezesResponse |
| 10 | +from ....core.serialization import convert_and_respect_annotation_metadata |
| 11 | +from ....core.pydantic_utilities import parse_obj_as |
| 12 | +from ....errors.bad_request_error import BadRequestError |
| 13 | +from ....types.error_body import ErrorBody |
| 14 | +from ....errors.unauthorized_error import UnauthorizedError |
| 15 | +from ....errors.unprocessable_entity_error import UnprocessableEntityError |
| 16 | +from json.decoder import JSONDecodeError |
| 17 | +from ....core.api_error import ApiError |
| 18 | +from ....core.client_wrapper import AsyncClientWrapper |
| 19 | + |
| 20 | +# this is used as the default value for optional parameters |
| 21 | +OMIT = typing.cast(typing.Any, ...) |
| 22 | + |
| 23 | + |
| 24 | +class FreezesClient: |
| 25 | + def __init__(self, *, client_wrapper: SyncClientWrapper): |
| 26 | + self._client_wrapper = client_wrapper |
| 27 | + |
| 28 | + def create( |
| 29 | + self, |
| 30 | + *, |
| 31 | + freezes: typing.Sequence[CreateStreakFreezesRequestFreezesItem], |
| 32 | + request_options: typing.Optional[RequestOptions] = None, |
| 33 | + ) -> CreateStreakFreezesResponse: |
| 34 | + """ |
| 35 | + Create streak freezes for multiple users. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + freezes : typing.Sequence[CreateStreakFreezesRequestFreezesItem] |
| 40 | + Array of freezes to create. Maximum 1,000 freezes per request. |
| 41 | +
|
| 42 | + request_options : typing.Optional[RequestOptions] |
| 43 | + Request-specific configuration. |
| 44 | +
|
| 45 | + Returns |
| 46 | + ------- |
| 47 | + CreateStreakFreezesResponse |
| 48 | + Successful operation |
| 49 | +
|
| 50 | + Examples |
| 51 | + -------- |
| 52 | + from trophy import TrophyApi |
| 53 | + from trophy.admin.streaks.freezes import CreateStreakFreezesRequestFreezesItem |
| 54 | +
|
| 55 | + client = TrophyApi( |
| 56 | + api_key="YOUR_API_KEY", |
| 57 | + ) |
| 58 | + client.admin.streaks.freezes.create( |
| 59 | + freezes=[ |
| 60 | + CreateStreakFreezesRequestFreezesItem( |
| 61 | + user_id="user-123", |
| 62 | + ), |
| 63 | + CreateStreakFreezesRequestFreezesItem( |
| 64 | + user_id="user-456", |
| 65 | + ), |
| 66 | + CreateStreakFreezesRequestFreezesItem( |
| 67 | + user_id="user-123", |
| 68 | + ), |
| 69 | + ], |
| 70 | + ) |
| 71 | + """ |
| 72 | + _response = self._client_wrapper.httpx_client.request( |
| 73 | + "streaks/freezes", |
| 74 | + base_url=self._client_wrapper.get_environment().admin, |
| 75 | + method="POST", |
| 76 | + json={ |
| 77 | + "freezes": convert_and_respect_annotation_metadata( |
| 78 | + object_=freezes, |
| 79 | + annotation=typing.Sequence[CreateStreakFreezesRequestFreezesItem], |
| 80 | + direction="write", |
| 81 | + ), |
| 82 | + }, |
| 83 | + headers={ |
| 84 | + "content-type": "application/json", |
| 85 | + }, |
| 86 | + request_options=request_options, |
| 87 | + omit=OMIT, |
| 88 | + ) |
| 89 | + try: |
| 90 | + if 200 <= _response.status_code < 300: |
| 91 | + return typing.cast( |
| 92 | + CreateStreakFreezesResponse, |
| 93 | + parse_obj_as( |
| 94 | + type_=CreateStreakFreezesResponse, # type: ignore |
| 95 | + object_=_response.json(), |
| 96 | + ), |
| 97 | + ) |
| 98 | + if _response.status_code == 400: |
| 99 | + raise BadRequestError( |
| 100 | + typing.cast( |
| 101 | + ErrorBody, |
| 102 | + parse_obj_as( |
| 103 | + type_=ErrorBody, # type: ignore |
| 104 | + object_=_response.json(), |
| 105 | + ), |
| 106 | + ) |
| 107 | + ) |
| 108 | + if _response.status_code == 401: |
| 109 | + raise UnauthorizedError( |
| 110 | + typing.cast( |
| 111 | + ErrorBody, |
| 112 | + parse_obj_as( |
| 113 | + type_=ErrorBody, # type: ignore |
| 114 | + object_=_response.json(), |
| 115 | + ), |
| 116 | + ) |
| 117 | + ) |
| 118 | + if _response.status_code == 422: |
| 119 | + raise UnprocessableEntityError( |
| 120 | + typing.cast( |
| 121 | + ErrorBody, |
| 122 | + parse_obj_as( |
| 123 | + type_=ErrorBody, # type: ignore |
| 124 | + object_=_response.json(), |
| 125 | + ), |
| 126 | + ) |
| 127 | + ) |
| 128 | + _response_json = _response.json() |
| 129 | + except JSONDecodeError: |
| 130 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 131 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
| 132 | + |
| 133 | + |
| 134 | +class AsyncFreezesClient: |
| 135 | + def __init__(self, *, client_wrapper: AsyncClientWrapper): |
| 136 | + self._client_wrapper = client_wrapper |
| 137 | + |
| 138 | + async def create( |
| 139 | + self, |
| 140 | + *, |
| 141 | + freezes: typing.Sequence[CreateStreakFreezesRequestFreezesItem], |
| 142 | + request_options: typing.Optional[RequestOptions] = None, |
| 143 | + ) -> CreateStreakFreezesResponse: |
| 144 | + """ |
| 145 | + Create streak freezes for multiple users. |
| 146 | +
|
| 147 | + Parameters |
| 148 | + ---------- |
| 149 | + freezes : typing.Sequence[CreateStreakFreezesRequestFreezesItem] |
| 150 | + Array of freezes to create. Maximum 1,000 freezes per request. |
| 151 | +
|
| 152 | + request_options : typing.Optional[RequestOptions] |
| 153 | + Request-specific configuration. |
| 154 | +
|
| 155 | + Returns |
| 156 | + ------- |
| 157 | + CreateStreakFreezesResponse |
| 158 | + Successful operation |
| 159 | +
|
| 160 | + Examples |
| 161 | + -------- |
| 162 | + import asyncio |
| 163 | +
|
| 164 | + from trophy import AsyncTrophyApi |
| 165 | + from trophy.admin.streaks.freezes import CreateStreakFreezesRequestFreezesItem |
| 166 | +
|
| 167 | + client = AsyncTrophyApi( |
| 168 | + api_key="YOUR_API_KEY", |
| 169 | + ) |
| 170 | +
|
| 171 | +
|
| 172 | + async def main() -> None: |
| 173 | + await client.admin.streaks.freezes.create( |
| 174 | + freezes=[ |
| 175 | + CreateStreakFreezesRequestFreezesItem( |
| 176 | + user_id="user-123", |
| 177 | + ), |
| 178 | + CreateStreakFreezesRequestFreezesItem( |
| 179 | + user_id="user-456", |
| 180 | + ), |
| 181 | + CreateStreakFreezesRequestFreezesItem( |
| 182 | + user_id="user-123", |
| 183 | + ), |
| 184 | + ], |
| 185 | + ) |
| 186 | +
|
| 187 | +
|
| 188 | + asyncio.run(main()) |
| 189 | + """ |
| 190 | + _response = await self._client_wrapper.httpx_client.request( |
| 191 | + "streaks/freezes", |
| 192 | + base_url=self._client_wrapper.get_environment().admin, |
| 193 | + method="POST", |
| 194 | + json={ |
| 195 | + "freezes": convert_and_respect_annotation_metadata( |
| 196 | + object_=freezes, |
| 197 | + annotation=typing.Sequence[CreateStreakFreezesRequestFreezesItem], |
| 198 | + direction="write", |
| 199 | + ), |
| 200 | + }, |
| 201 | + headers={ |
| 202 | + "content-type": "application/json", |
| 203 | + }, |
| 204 | + request_options=request_options, |
| 205 | + omit=OMIT, |
| 206 | + ) |
| 207 | + try: |
| 208 | + if 200 <= _response.status_code < 300: |
| 209 | + return typing.cast( |
| 210 | + CreateStreakFreezesResponse, |
| 211 | + parse_obj_as( |
| 212 | + type_=CreateStreakFreezesResponse, # type: ignore |
| 213 | + object_=_response.json(), |
| 214 | + ), |
| 215 | + ) |
| 216 | + if _response.status_code == 400: |
| 217 | + raise BadRequestError( |
| 218 | + typing.cast( |
| 219 | + ErrorBody, |
| 220 | + parse_obj_as( |
| 221 | + type_=ErrorBody, # type: ignore |
| 222 | + object_=_response.json(), |
| 223 | + ), |
| 224 | + ) |
| 225 | + ) |
| 226 | + if _response.status_code == 401: |
| 227 | + raise UnauthorizedError( |
| 228 | + typing.cast( |
| 229 | + ErrorBody, |
| 230 | + parse_obj_as( |
| 231 | + type_=ErrorBody, # type: ignore |
| 232 | + object_=_response.json(), |
| 233 | + ), |
| 234 | + ) |
| 235 | + ) |
| 236 | + if _response.status_code == 422: |
| 237 | + raise UnprocessableEntityError( |
| 238 | + typing.cast( |
| 239 | + ErrorBody, |
| 240 | + parse_obj_as( |
| 241 | + type_=ErrorBody, # type: ignore |
| 242 | + object_=_response.json(), |
| 243 | + ), |
| 244 | + ) |
| 245 | + ) |
| 246 | + _response_json = _response.json() |
| 247 | + except JSONDecodeError: |
| 248 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 249 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
0 commit comments