fastspec 0.2.7__tar.gz → 0.2.8__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastspec
3
- Version: 0.2.7
3
+ Version: 0.2.8
4
4
  Summary: Dynamic OpenAPI, Discovery, and GraphQL spec client for Python — turn any API spec into a fully-typed async client with attribute chaining, streaming, and file uploads
5
5
  Author-email: Kerem Turgutlu <keremturgutlu@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -4,4 +4,4 @@ Modules:
4
4
 
5
5
  - `fastspec.gql`: Build GraphQL queries by field-checked attribute chaining, reuse distilled schemas, and combine queries into batched requests"""
6
6
 
7
- __version__ = "0.2.7"
7
+ __version__ = "0.2.8"
@@ -74,6 +74,7 @@ d = { 'settings': { 'branch': 'main',
74
74
  'fastspec.oapi.SyncOpFunc': ('oapi.html#syncopfunc', 'fastspec/oapi.py'),
75
75
  'fastspec.oapi.SyncOpFunc.__call__': ('oapi.html#syncopfunc.__call__', 'fastspec/oapi.py'),
76
76
  'fastspec.oapi._examples_doc': ('oapi.html#_examples_doc', 'fastspec/oapi.py'),
77
+ 'fastspec.oapi._file_arg': ('oapi.html#_file_arg', 'fastspec/oapi.py'),
77
78
  'fastspec.oapi._join_url': ('oapi.html#_join_url', 'fastspec/oapi.py'),
78
79
  'fastspec.oapi._path': ('oapi.html#_path', 'fastspec/oapi.py'),
79
80
  'fastspec.oapi.op_func': ('oapi.html#op_func', 'fastspec/oapi.py')},
@@ -98,6 +99,7 @@ d = { 'settings': { 'branch': 'main',
98
99
  'fastspec.spec._discovery_collect_params': ('spec.html#_discovery_collect_params', 'fastspec/spec.py'),
99
100
  'fastspec.spec._first_url': ('spec.html#_first_url', 'fastspec/spec.py'),
100
101
  'fastspec.spec._group_name': ('spec.html#_group_name', 'fastspec/spec.py'),
102
+ 'fastspec.spec._is_binary': ('spec.html#_is_binary', 'fastspec/spec.py'),
101
103
  'fastspec.spec._media_twin': ('spec.html#_media_twin', 'fastspec/spec.py'),
102
104
  'fastspec.spec._op_docs_url': ('spec.html#_op_docs_url', 'fastspec/spec.py'),
103
105
  'fastspec.spec._path_params': ('spec.html#_path_params', 'fastspec/spec.py'),
@@ -74,6 +74,11 @@ def _bind(self:OpFunc, args, kwargs):
74
74
  return kwargs
75
75
 
76
76
  # %% ../nbs/04_oapi.ipynb #9327d89c
77
+ def _file_arg(v):
78
+ "Convert a `Path` to an httpx `(filename, bytes, content_type)` tuple; pass other file inputs through."
79
+ if isinstance(v, Path): return (v.name, v.read_bytes(), mimetypes.guess_type(v.name)[0] or 'application/octet-stream')
80
+ return v
81
+
77
82
  @patch
78
83
  def _split(self:OpFunc, kwargs):
79
84
  "Split kwargs into route/query/body/files + control kwargs."
@@ -88,7 +93,8 @@ def _split(self:OpFunc, kwargs):
88
93
  route, query, body, files = {}, {}, {}, {}
89
94
  for orig,v in params.items():
90
95
  if orig in self.route_params: route[orig] = v
91
- elif orig in self.file_params: files[orig] = v
96
+ elif orig in self.file_params:
97
+ if v is not None: files[orig] = _file_arg(v)
92
98
  elif orig in self.query_params: query[orig] = v
93
99
  elif orig in self.body_params: body[orig] = v
94
100
 
@@ -127,14 +127,28 @@ def _schema_props_required(schema, spec):
127
127
 
128
128
  # %% ../nbs/03_spec.ipynb #2319e3a4
129
129
  _type_map = dict(string=str, integer=int, number=float, boolean=bool, array=list, object=dict)
130
+ _text_media = {'application/json', 'application/xml', 'application/x-www-form-urlencoded'}
131
+
132
+ def _is_binary(schema, spec):
133
+ "True when `schema` describes raw binary data (OpenAPI 3.0 `format: binary` or 3.1 binary `contentMediaType`)."
134
+ if not isinstance(schema, dict): return False
135
+ schema = _resolve_obj(schema, spec)
136
+ if schema.get("format") == "binary": return True
137
+ mt = schema.get("contentMediaType")
138
+ if mt and not schema.get("contentEncoding") and not mt.startswith("text/") and mt not in _text_media: return True
139
+ for key in ("anyOf", "oneOf"):
140
+ subs = [_resolve_obj(s, spec) for s in schema.get(key, [])]
141
+ non_null = [s for s in subs if s.get("type") != "null"]
142
+ if non_null and all(_is_binary(s, spec) for s in non_null): return True
143
+ return False
130
144
 
131
145
  def _schema_py_type(schema, spec):
132
146
  "Best-effort Python type from a JSON Schema fragment."
133
147
  if not isinstance(schema, dict): return None
134
148
  schema = _resolve_obj(schema, spec)
149
+ if _is_binary(schema, spec): return bytes
135
150
  t = schema.get("type")
136
- if isinstance(t, L): t = first(t.filter(lambda x: x != 'null'))
137
- if t == "string" and schema.get("format") == "binary": return bytes
151
+ if isinstance(t, (list, L)): t = first(o for o in t if o != 'null')
138
152
  if t in _type_map: return _type_map[t]
139
153
  if t == "null": return type(None)
140
154
  for key in ("oneOf", "anyOf", "allOf"):
@@ -208,7 +222,7 @@ def _body_params(op, spec):
208
222
  return AttrDict(body_params=[], file_params=[], required_params=set(), param_types={},
209
223
  param_docs={}, param_defaults={}, request_content_type=ct, body_examples=bexs)
210
224
  props, req = _schema_props_required(schema, spec)
211
- fparams = [k for k,v in props.items() if _resolve_obj(v, spec).get("format") == "binary"]
225
+ fparams = [k for k,v in props.items() if _is_binary(v, spec)]
212
226
  bparams = [k for k in props if k not in fparams]
213
227
  ptypes = {k: _schema_py_type(v, spec) for k,v in props.items()}
214
228
  pdocs = {k: d for k,v in props.items() if (d := _prop_desc(v, spec))}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastspec
3
- Version: 0.2.7
3
+ Version: 0.2.8
4
4
  Summary: Dynamic OpenAPI, Discovery, and GraphQL spec client for Python — turn any API spec into a fully-typed async client with attribute chaining, streaming, and file uploads
5
5
  Author-email: Kerem Turgutlu <keremturgutlu@gmail.com>
6
6
  License-Expression: Apache-2.0
File without changes
File without changes
File without changes
File without changes
File without changes