openapi-httpx-client 0.4.1__tar.gz → 0.4.2__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.1
2
2
  Name: openapi-httpx-client
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: A Python client for OpenAPI specifications using httpx
5
5
  Home-page: https://github.com/lloydzhou/openapiclient
6
6
  Author: lloydzhou
@@ -47,6 +47,11 @@ async def main():
47
47
  pet = await client.getPetById(petId=1)
48
48
  print(f"Status: {pet['status']}")
49
49
  print(f"Pet data: {pet['data']}")
50
+
51
+ # Call operations directly as methods, using positional arguments, can using in path and query
52
+ pet = await client.getPetById(1)
53
+ print(f"Status: {pet['status']}")
54
+ print(f"Pet data: {pet['data']}")
50
55
 
51
56
  # Alternative way to call methods
52
57
  pet = await client("getPetById", petId=2)
@@ -32,6 +32,11 @@ async def main():
32
32
  pet = await client.getPetById(petId=1)
33
33
  print(f"Status: {pet['status']}")
34
34
  print(f"Pet data: {pet['data']}")
35
+
36
+ # Call operations directly as methods, using positional arguments, can using in path and query
37
+ pet = await client.getPetById(1)
38
+ print(f"Status: {pet['status']}")
39
+ print(f"Pet data: {pet['data']}")
35
40
 
36
41
  # Alternative way to call methods
37
42
  pet = await client("getPetById", petId=2)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: openapi-httpx-client
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: A Python client for OpenAPI specifications using httpx
5
5
  Home-page: https://github.com/lloydzhou/openapiclient
6
6
  Author: lloydzhou
@@ -47,6 +47,11 @@ async def main():
47
47
  pet = await client.getPetById(petId=1)
48
48
  print(f"Status: {pet['status']}")
49
49
  print(f"Pet data: {pet['data']}")
50
+
51
+ # Call operations directly as methods, using positional arguments, can using in path and query
52
+ pet = await client.getPetById(1)
53
+ print(f"Status: {pet['status']}")
54
+ print(f"Pet data: {pet['data']}")
50
55
 
51
56
  # Alternative way to call methods
52
57
  pet = await client("getPetById", petId=2)
@@ -289,38 +289,43 @@ class OpenAPIClient:
289
289
 
290
290
  def create_tool(self, operation_id, operation, all_references):
291
291
  """Create an AI tool description from operation data"""
292
- # Get parameters from the request body schema only for json content
292
+ # Get parameters from the request body schema
293
293
  body = operation.get('requestBody', {})
294
- schema = body.get('content', {}).get('application/json', {}).get('schema', {})
295
- parameters = {
296
- "type": "object",
297
- "required": ['body'] if body.get("required", False) else [],
298
- "description": body.get('description', ''),
299
- "properties": {
300
- "body": self.resolve_schema_ref(schema, all_references) if schema else {},
301
- }
302
- }
294
+ content = body.get('content', {})
295
+ schema = content.get('application/json', {}).get('schema', {}) or content.get('application/xml', {}).get('schema', {}) or content.get('application/x-www-form-urlencoded', {}).get('schema', {})
296
+ json_schema = self.resolve_schema_ref(schema, all_references) if schema else { "type": "object", "properties": {} }
297
+
298
+ if not json_schema.get('description'):
299
+ json_schema['description'] = body.get('description', '')
300
+
303
301
  # add parameters from path and query
304
- for parameter in operation.get('parameters', []):
305
- name = parameter.get('name')
306
- if parameter.get('required', False):
307
- parameters["required"].append(name)
308
- item = {
309
- "type": parameter.get('schema', {}).get('type', 'string'),
310
- "description": parameter.get('description', ''),
311
- }
312
- # Add format, enum, and example if available
313
- for key in ['format', 'enum', 'example']:
314
- if parameter.get('schema', {}).get(key):
315
- item[key] = parameter.get('schema', {}).get(key)
316
- parameters["properties"][name] = item
302
+ parameters = operation.get('parameters', [])
303
+ if len(parameters) > 0:
304
+ if not json_schema.get('required'):
305
+ json_schema['required'] = []
306
+ if not json_schema.get('properties'):
307
+ json_schema['properties'] = {}
308
+ for parameter in parameters:
309
+ name = parameter.get('name')
310
+ if parameter.get('required', False):
311
+ json_schema["required"].append(name)
312
+
313
+ parameter_schema = {
314
+ "type": parameter.get('schema', {}).get('type', 'string'),
315
+ "description": parameter.get('description', ''),
316
+ }
317
+ # Add format, enum, and example if available
318
+ for key in ['format', 'enum', 'example']:
319
+ if parameter.get('schema', {}).get(key):
320
+ parameter_schema[key] = parameter.get('schema', {}).get(key)
321
+ json_schema["properties"][name] = parameter_schema
317
322
 
318
323
  return {
319
324
  "type": "function",
320
325
  "function": {
321
326
  "name": operation_id,
322
327
  "description": operation.get('summary', '') or operation.get('description', ''),
323
- "parameters": parameters,
328
+ "parameters": json_schema,
324
329
  }
325
330
  }
326
331
 
@@ -374,13 +379,14 @@ class OpenAPIClient:
374
379
  client_instance.paths = paths
375
380
  client_instance.tools = tools
376
381
 
377
- def _prepare_request_params(self, path, operation, kwargs):
382
+ def _prepare_request_params(self, path, operation, args, kwargs):
378
383
  """
379
384
  Prepare request parameters for an API operation.
380
385
 
381
386
  Args:
382
387
  path: The path template
383
388
  operation: Operation object
389
+ args: Positional arguments passed to the operation
384
390
  kwargs: Keyword arguments passed to the operation
385
391
 
386
392
  Returns:
@@ -397,6 +403,8 @@ class OpenAPIClient:
397
403
  name = param.get('name')
398
404
  if name in kwargs:
399
405
  path_params[name] = kwargs.pop(name)
406
+ elif len(args) > 0:
407
+ path_params[name] = args.pop(0) # Pop the first positional argument
400
408
 
401
409
  # Replace path parameters in the URL
402
410
  for name, value in path_params.items():
@@ -412,6 +420,8 @@ class OpenAPIClient:
412
420
  name = param.get('name')
413
421
  if name in kwargs:
414
422
  query_params[name] = kwargs.pop(name)
423
+ elif len(args) > 0:
424
+ query_params[name] = args.pop(0) # Pop the first positional argument
415
425
 
416
426
  # Handle headers
417
427
  headers = kwargs.pop('headers', {})
@@ -419,7 +429,7 @@ class OpenAPIClient:
419
429
  # Handle request body
420
430
  body = kwargs.pop('data', None) or kwargs.pop('body', None)
421
431
  # json body
422
- if not body and len(kwargs) > 0 and operation.get('requestBody', {}).get('content', {}).get('application/json'):
432
+ if not body and len(kwargs) > 0 and operation.get('requestBody', {}).get('content', {}):
423
433
  body = kwargs.copy()
424
434
  kwargs.clear() # Clear the kwargs after using them as body
425
435
 
@@ -466,7 +476,7 @@ class OpenAPIClient:
466
476
  async def operation_method(*args, **kwargs):
467
477
  # Prepare request parameters
468
478
  full_url, query_params, body, headers, remaining_kwargs = self._prepare_request_params(
469
- path, operation, kwargs.copy()
479
+ path, operation, list(args), kwargs.copy()
470
480
  )
471
481
 
472
482
  # Make the async request
@@ -485,7 +495,7 @@ class OpenAPIClient:
485
495
  def operation_method(*args, **kwargs):
486
496
  # Prepare request parameters
487
497
  full_url, query_params, body, headers, remaining_kwargs = self._prepare_request_params(
488
- path, operation, kwargs.copy()
498
+ path, operation, list(args), kwargs.copy()
489
499
  )
490
500
 
491
501
  # Make the sync request
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name="openapi-httpx-client",
8
- version="0.4.1",
8
+ version="0.4.2",
9
9
  author="lloydzhou",
10
10
  author_email="lloydzhou@qq.com",
11
11
  description="A Python client for OpenAPI specifications using httpx",