dub 0.26.12__py3-none-any.whl → 0.27.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dub/track.py CHANGED
@@ -103,63 +103,58 @@ class Track(BaseSDK):
103
103
 
104
104
  response_data: Any = None
105
105
  if utils.match_response(http_res, "200", "application/json"):
106
- return utils.unmarshal_json(
107
- http_res.text, Optional[operations.TrackLeadResponseBody]
106
+ return utils.unmarshal_json_response(
107
+ Optional[operations.TrackLeadResponseBody], http_res
108
108
  )
109
109
  if utils.match_response(http_res, "400", "application/json"):
110
- response_data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
111
- raise errors.BadRequest(data=response_data)
110
+ response_data = utils.unmarshal_json_response(
111
+ errors.BadRequestData, http_res
112
+ )
113
+ raise errors.BadRequest(response_data, http_res)
112
114
  if utils.match_response(http_res, "401", "application/json"):
113
- response_data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
114
- raise errors.Unauthorized(data=response_data)
115
+ response_data = utils.unmarshal_json_response(
116
+ errors.UnauthorizedData, http_res
117
+ )
118
+ raise errors.Unauthorized(response_data, http_res)
115
119
  if utils.match_response(http_res, "403", "application/json"):
116
- response_data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
117
- raise errors.Forbidden(data=response_data)
120
+ response_data = utils.unmarshal_json_response(
121
+ errors.ForbiddenData, http_res
122
+ )
123
+ raise errors.Forbidden(response_data, http_res)
118
124
  if utils.match_response(http_res, "404", "application/json"):
119
- response_data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
120
- raise errors.NotFound(data=response_data)
125
+ response_data = utils.unmarshal_json_response(errors.NotFoundData, http_res)
126
+ raise errors.NotFound(response_data, http_res)
121
127
  if utils.match_response(http_res, "409", "application/json"):
122
- response_data = utils.unmarshal_json(http_res.text, errors.ConflictData)
123
- raise errors.Conflict(data=response_data)
128
+ response_data = utils.unmarshal_json_response(errors.ConflictData, http_res)
129
+ raise errors.Conflict(response_data, http_res)
124
130
  if utils.match_response(http_res, "410", "application/json"):
125
- response_data = utils.unmarshal_json(
126
- http_res.text, errors.InviteExpiredData
131
+ response_data = utils.unmarshal_json_response(
132
+ errors.InviteExpiredData, http_res
127
133
  )
128
- raise errors.InviteExpired(data=response_data)
134
+ raise errors.InviteExpired(response_data, http_res)
129
135
  if utils.match_response(http_res, "422", "application/json"):
130
- response_data = utils.unmarshal_json(
131
- http_res.text, errors.UnprocessableEntityData
136
+ response_data = utils.unmarshal_json_response(
137
+ errors.UnprocessableEntityData, http_res
132
138
  )
133
- raise errors.UnprocessableEntity(data=response_data)
139
+ raise errors.UnprocessableEntity(response_data, http_res)
134
140
  if utils.match_response(http_res, "429", "application/json"):
135
- response_data = utils.unmarshal_json(
136
- http_res.text, errors.RateLimitExceededData
141
+ response_data = utils.unmarshal_json_response(
142
+ errors.RateLimitExceededData, http_res
137
143
  )
138
- raise errors.RateLimitExceeded(data=response_data)
144
+ raise errors.RateLimitExceeded(response_data, http_res)
139
145
  if utils.match_response(http_res, "500", "application/json"):
140
- response_data = utils.unmarshal_json(
141
- http_res.text, errors.InternalServerErrorData
146
+ response_data = utils.unmarshal_json_response(
147
+ errors.InternalServerErrorData, http_res
142
148
  )
143
- raise errors.InternalServerError(data=response_data)
149
+ raise errors.InternalServerError(response_data, http_res)
144
150
  if utils.match_response(http_res, "4XX", "*"):
145
151
  http_res_text = utils.stream_to_text(http_res)
146
- raise errors.SDKError(
147
- "API error occurred", http_res.status_code, http_res_text, http_res
148
- )
152
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
149
153
  if utils.match_response(http_res, "5XX", "*"):
150
154
  http_res_text = utils.stream_to_text(http_res)
151
- raise errors.SDKError(
152
- "API error occurred", http_res.status_code, http_res_text, http_res
153
- )
155
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
154
156
 
155
- content_type = http_res.headers.get("Content-Type")
156
- http_res_text = utils.stream_to_text(http_res)
157
- raise errors.SDKError(
158
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
159
- http_res.status_code,
160
- http_res_text,
161
- http_res,
162
- )
157
+ raise errors.SDKError("Unexpected response received", http_res)
163
158
 
164
159
  async def lead_async(
165
160
  self,
@@ -255,63 +250,58 @@ class Track(BaseSDK):
255
250
 
256
251
  response_data: Any = None
257
252
  if utils.match_response(http_res, "200", "application/json"):
258
- return utils.unmarshal_json(
259
- http_res.text, Optional[operations.TrackLeadResponseBody]
253
+ return utils.unmarshal_json_response(
254
+ Optional[operations.TrackLeadResponseBody], http_res
260
255
  )
261
256
  if utils.match_response(http_res, "400", "application/json"):
262
- response_data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
263
- raise errors.BadRequest(data=response_data)
257
+ response_data = utils.unmarshal_json_response(
258
+ errors.BadRequestData, http_res
259
+ )
260
+ raise errors.BadRequest(response_data, http_res)
264
261
  if utils.match_response(http_res, "401", "application/json"):
265
- response_data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
266
- raise errors.Unauthorized(data=response_data)
262
+ response_data = utils.unmarshal_json_response(
263
+ errors.UnauthorizedData, http_res
264
+ )
265
+ raise errors.Unauthorized(response_data, http_res)
267
266
  if utils.match_response(http_res, "403", "application/json"):
268
- response_data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
269
- raise errors.Forbidden(data=response_data)
267
+ response_data = utils.unmarshal_json_response(
268
+ errors.ForbiddenData, http_res
269
+ )
270
+ raise errors.Forbidden(response_data, http_res)
270
271
  if utils.match_response(http_res, "404", "application/json"):
271
- response_data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
272
- raise errors.NotFound(data=response_data)
272
+ response_data = utils.unmarshal_json_response(errors.NotFoundData, http_res)
273
+ raise errors.NotFound(response_data, http_res)
273
274
  if utils.match_response(http_res, "409", "application/json"):
274
- response_data = utils.unmarshal_json(http_res.text, errors.ConflictData)
275
- raise errors.Conflict(data=response_data)
275
+ response_data = utils.unmarshal_json_response(errors.ConflictData, http_res)
276
+ raise errors.Conflict(response_data, http_res)
276
277
  if utils.match_response(http_res, "410", "application/json"):
277
- response_data = utils.unmarshal_json(
278
- http_res.text, errors.InviteExpiredData
278
+ response_data = utils.unmarshal_json_response(
279
+ errors.InviteExpiredData, http_res
279
280
  )
280
- raise errors.InviteExpired(data=response_data)
281
+ raise errors.InviteExpired(response_data, http_res)
281
282
  if utils.match_response(http_res, "422", "application/json"):
282
- response_data = utils.unmarshal_json(
283
- http_res.text, errors.UnprocessableEntityData
283
+ response_data = utils.unmarshal_json_response(
284
+ errors.UnprocessableEntityData, http_res
284
285
  )
285
- raise errors.UnprocessableEntity(data=response_data)
286
+ raise errors.UnprocessableEntity(response_data, http_res)
286
287
  if utils.match_response(http_res, "429", "application/json"):
287
- response_data = utils.unmarshal_json(
288
- http_res.text, errors.RateLimitExceededData
288
+ response_data = utils.unmarshal_json_response(
289
+ errors.RateLimitExceededData, http_res
289
290
  )
290
- raise errors.RateLimitExceeded(data=response_data)
291
+ raise errors.RateLimitExceeded(response_data, http_res)
291
292
  if utils.match_response(http_res, "500", "application/json"):
292
- response_data = utils.unmarshal_json(
293
- http_res.text, errors.InternalServerErrorData
293
+ response_data = utils.unmarshal_json_response(
294
+ errors.InternalServerErrorData, http_res
294
295
  )
295
- raise errors.InternalServerError(data=response_data)
296
+ raise errors.InternalServerError(response_data, http_res)
296
297
  if utils.match_response(http_res, "4XX", "*"):
297
298
  http_res_text = await utils.stream_to_text_async(http_res)
298
- raise errors.SDKError(
299
- "API error occurred", http_res.status_code, http_res_text, http_res
300
- )
299
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
301
300
  if utils.match_response(http_res, "5XX", "*"):
302
301
  http_res_text = await utils.stream_to_text_async(http_res)
303
- raise errors.SDKError(
304
- "API error occurred", http_res.status_code, http_res_text, http_res
305
- )
302
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
306
303
 
307
- content_type = http_res.headers.get("Content-Type")
308
- http_res_text = await utils.stream_to_text_async(http_res)
309
- raise errors.SDKError(
310
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
311
- http_res.status_code,
312
- http_res_text,
313
- http_res,
314
- )
304
+ raise errors.SDKError("Unexpected response received", http_res)
315
305
 
316
306
  def sale(
317
307
  self,
@@ -407,63 +397,58 @@ class Track(BaseSDK):
407
397
 
408
398
  response_data: Any = None
409
399
  if utils.match_response(http_res, "200", "application/json"):
410
- return utils.unmarshal_json(
411
- http_res.text, Optional[operations.TrackSaleResponseBody]
400
+ return utils.unmarshal_json_response(
401
+ Optional[operations.TrackSaleResponseBody], http_res
412
402
  )
413
403
  if utils.match_response(http_res, "400", "application/json"):
414
- response_data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
415
- raise errors.BadRequest(data=response_data)
404
+ response_data = utils.unmarshal_json_response(
405
+ errors.BadRequestData, http_res
406
+ )
407
+ raise errors.BadRequest(response_data, http_res)
416
408
  if utils.match_response(http_res, "401", "application/json"):
417
- response_data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
418
- raise errors.Unauthorized(data=response_data)
409
+ response_data = utils.unmarshal_json_response(
410
+ errors.UnauthorizedData, http_res
411
+ )
412
+ raise errors.Unauthorized(response_data, http_res)
419
413
  if utils.match_response(http_res, "403", "application/json"):
420
- response_data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
421
- raise errors.Forbidden(data=response_data)
414
+ response_data = utils.unmarshal_json_response(
415
+ errors.ForbiddenData, http_res
416
+ )
417
+ raise errors.Forbidden(response_data, http_res)
422
418
  if utils.match_response(http_res, "404", "application/json"):
423
- response_data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
424
- raise errors.NotFound(data=response_data)
419
+ response_data = utils.unmarshal_json_response(errors.NotFoundData, http_res)
420
+ raise errors.NotFound(response_data, http_res)
425
421
  if utils.match_response(http_res, "409", "application/json"):
426
- response_data = utils.unmarshal_json(http_res.text, errors.ConflictData)
427
- raise errors.Conflict(data=response_data)
422
+ response_data = utils.unmarshal_json_response(errors.ConflictData, http_res)
423
+ raise errors.Conflict(response_data, http_res)
428
424
  if utils.match_response(http_res, "410", "application/json"):
429
- response_data = utils.unmarshal_json(
430
- http_res.text, errors.InviteExpiredData
425
+ response_data = utils.unmarshal_json_response(
426
+ errors.InviteExpiredData, http_res
431
427
  )
432
- raise errors.InviteExpired(data=response_data)
428
+ raise errors.InviteExpired(response_data, http_res)
433
429
  if utils.match_response(http_res, "422", "application/json"):
434
- response_data = utils.unmarshal_json(
435
- http_res.text, errors.UnprocessableEntityData
430
+ response_data = utils.unmarshal_json_response(
431
+ errors.UnprocessableEntityData, http_res
436
432
  )
437
- raise errors.UnprocessableEntity(data=response_data)
433
+ raise errors.UnprocessableEntity(response_data, http_res)
438
434
  if utils.match_response(http_res, "429", "application/json"):
439
- response_data = utils.unmarshal_json(
440
- http_res.text, errors.RateLimitExceededData
435
+ response_data = utils.unmarshal_json_response(
436
+ errors.RateLimitExceededData, http_res
441
437
  )
442
- raise errors.RateLimitExceeded(data=response_data)
438
+ raise errors.RateLimitExceeded(response_data, http_res)
443
439
  if utils.match_response(http_res, "500", "application/json"):
444
- response_data = utils.unmarshal_json(
445
- http_res.text, errors.InternalServerErrorData
440
+ response_data = utils.unmarshal_json_response(
441
+ errors.InternalServerErrorData, http_res
446
442
  )
447
- raise errors.InternalServerError(data=response_data)
443
+ raise errors.InternalServerError(response_data, http_res)
448
444
  if utils.match_response(http_res, "4XX", "*"):
449
445
  http_res_text = utils.stream_to_text(http_res)
450
- raise errors.SDKError(
451
- "API error occurred", http_res.status_code, http_res_text, http_res
452
- )
446
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
453
447
  if utils.match_response(http_res, "5XX", "*"):
454
448
  http_res_text = utils.stream_to_text(http_res)
455
- raise errors.SDKError(
456
- "API error occurred", http_res.status_code, http_res_text, http_res
457
- )
449
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
458
450
 
459
- content_type = http_res.headers.get("Content-Type")
460
- http_res_text = utils.stream_to_text(http_res)
461
- raise errors.SDKError(
462
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
463
- http_res.status_code,
464
- http_res_text,
465
- http_res,
466
- )
451
+ raise errors.SDKError("Unexpected response received", http_res)
467
452
 
468
453
  async def sale_async(
469
454
  self,
@@ -559,60 +544,55 @@ class Track(BaseSDK):
559
544
 
560
545
  response_data: Any = None
561
546
  if utils.match_response(http_res, "200", "application/json"):
562
- return utils.unmarshal_json(
563
- http_res.text, Optional[operations.TrackSaleResponseBody]
547
+ return utils.unmarshal_json_response(
548
+ Optional[operations.TrackSaleResponseBody], http_res
564
549
  )
565
550
  if utils.match_response(http_res, "400", "application/json"):
566
- response_data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
567
- raise errors.BadRequest(data=response_data)
551
+ response_data = utils.unmarshal_json_response(
552
+ errors.BadRequestData, http_res
553
+ )
554
+ raise errors.BadRequest(response_data, http_res)
568
555
  if utils.match_response(http_res, "401", "application/json"):
569
- response_data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
570
- raise errors.Unauthorized(data=response_data)
556
+ response_data = utils.unmarshal_json_response(
557
+ errors.UnauthorizedData, http_res
558
+ )
559
+ raise errors.Unauthorized(response_data, http_res)
571
560
  if utils.match_response(http_res, "403", "application/json"):
572
- response_data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
573
- raise errors.Forbidden(data=response_data)
561
+ response_data = utils.unmarshal_json_response(
562
+ errors.ForbiddenData, http_res
563
+ )
564
+ raise errors.Forbidden(response_data, http_res)
574
565
  if utils.match_response(http_res, "404", "application/json"):
575
- response_data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
576
- raise errors.NotFound(data=response_data)
566
+ response_data = utils.unmarshal_json_response(errors.NotFoundData, http_res)
567
+ raise errors.NotFound(response_data, http_res)
577
568
  if utils.match_response(http_res, "409", "application/json"):
578
- response_data = utils.unmarshal_json(http_res.text, errors.ConflictData)
579
- raise errors.Conflict(data=response_data)
569
+ response_data = utils.unmarshal_json_response(errors.ConflictData, http_res)
570
+ raise errors.Conflict(response_data, http_res)
580
571
  if utils.match_response(http_res, "410", "application/json"):
581
- response_data = utils.unmarshal_json(
582
- http_res.text, errors.InviteExpiredData
572
+ response_data = utils.unmarshal_json_response(
573
+ errors.InviteExpiredData, http_res
583
574
  )
584
- raise errors.InviteExpired(data=response_data)
575
+ raise errors.InviteExpired(response_data, http_res)
585
576
  if utils.match_response(http_res, "422", "application/json"):
586
- response_data = utils.unmarshal_json(
587
- http_res.text, errors.UnprocessableEntityData
577
+ response_data = utils.unmarshal_json_response(
578
+ errors.UnprocessableEntityData, http_res
588
579
  )
589
- raise errors.UnprocessableEntity(data=response_data)
580
+ raise errors.UnprocessableEntity(response_data, http_res)
590
581
  if utils.match_response(http_res, "429", "application/json"):
591
- response_data = utils.unmarshal_json(
592
- http_res.text, errors.RateLimitExceededData
582
+ response_data = utils.unmarshal_json_response(
583
+ errors.RateLimitExceededData, http_res
593
584
  )
594
- raise errors.RateLimitExceeded(data=response_data)
585
+ raise errors.RateLimitExceeded(response_data, http_res)
595
586
  if utils.match_response(http_res, "500", "application/json"):
596
- response_data = utils.unmarshal_json(
597
- http_res.text, errors.InternalServerErrorData
587
+ response_data = utils.unmarshal_json_response(
588
+ errors.InternalServerErrorData, http_res
598
589
  )
599
- raise errors.InternalServerError(data=response_data)
590
+ raise errors.InternalServerError(response_data, http_res)
600
591
  if utils.match_response(http_res, "4XX", "*"):
601
592
  http_res_text = await utils.stream_to_text_async(http_res)
602
- raise errors.SDKError(
603
- "API error occurred", http_res.status_code, http_res_text, http_res
604
- )
593
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
605
594
  if utils.match_response(http_res, "5XX", "*"):
606
595
  http_res_text = await utils.stream_to_text_async(http_res)
607
- raise errors.SDKError(
608
- "API error occurred", http_res.status_code, http_res_text, http_res
609
- )
596
+ raise errors.SDKError("API error occurred", http_res, http_res_text)
610
597
 
611
- content_type = http_res.headers.get("Content-Type")
612
- http_res_text = await utils.stream_to_text_async(http_res)
613
- raise errors.SDKError(
614
- f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
615
- http_res.status_code,
616
- http_res_text,
617
- http_res,
618
- )
598
+ raise errors.SDKError("Unexpected response received", http_res)
dub/utils/__init__.py CHANGED
@@ -28,6 +28,7 @@ if TYPE_CHECKING:
28
28
  marshal_json,
29
29
  unmarshal,
30
30
  unmarshal_json,
31
+ unmarshal_json_response,
31
32
  serialize_decimal,
32
33
  serialize_float,
33
34
  serialize_int,
@@ -96,6 +97,7 @@ __all__ = [
96
97
  "template_url",
97
98
  "unmarshal",
98
99
  "unmarshal_json",
100
+ "unmarshal_json_response",
99
101
  "validate_decimal",
100
102
  "validate_const",
101
103
  "validate_float",
@@ -149,6 +151,7 @@ _dynamic_imports: dict[str, str] = {
149
151
  "template_url": ".url",
150
152
  "unmarshal": ".serializers",
151
153
  "unmarshal_json": ".serializers",
154
+ "unmarshal_json_response": ".serializers",
152
155
  "validate_decimal": ".serializers",
153
156
  "validate_const": ".serializers",
154
157
  "validate_float": ".serializers",
dub/utils/serializers.py CHANGED
@@ -4,7 +4,7 @@ from decimal import Decimal
4
4
  import functools
5
5
  import json
6
6
  import typing
7
- from typing import Any, Dict, List, Tuple, Union, get_args
7
+ from typing import Any, Dict, List, Optional, Tuple, Union, get_args
8
8
  import typing_extensions
9
9
  from typing_extensions import get_origin
10
10
 
@@ -13,6 +13,7 @@ from pydantic import ConfigDict, create_model
13
13
  from pydantic_core import from_json
14
14
 
15
15
  from ..types.basemodel import BaseModel, Nullable, OptionalNullable, Unset
16
+ from dub.models import errors
16
17
 
17
18
 
18
19
  def serialize_decimal(as_str: bool):
@@ -140,6 +141,22 @@ def unmarshal_json(raw, typ: Any) -> Any:
140
141
  return unmarshal(from_json(raw), typ)
141
142
 
142
143
 
144
+ def unmarshal_json_response(
145
+ typ: Any, http_res: httpx.Response, body: Optional[str] = None
146
+ ) -> Any:
147
+ if body is None:
148
+ body = http_res.text
149
+ try:
150
+ return unmarshal_json(body, typ)
151
+ except Exception as e:
152
+ raise errors.ResponseValidationError(
153
+ "Response validation failed",
154
+ http_res,
155
+ e,
156
+ body,
157
+ ) from e
158
+
159
+
143
160
  def unmarshal(val, typ: Any) -> Any:
144
161
  unmarshaller = create_model(
145
162
  "Unmarshaller",
@@ -192,7 +209,9 @@ def is_union(obj: object) -> bool:
192
209
  """
193
210
  Returns True if the given object is a typing.Union or typing_extensions.Union.
194
211
  """
195
- return any(obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union"))
212
+ return any(
213
+ obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union")
214
+ )
196
215
 
197
216
 
198
217
  def stream_to_text(stream: httpx.Response) -> str:
@@ -245,4 +264,3 @@ def _get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]:
245
264
  f"Neither typing nor typing_extensions has an object called {name!r}"
246
265
  )
247
266
  return result
248
-