I used Bravado to create a Python client to a REST API for the petstore.
I need to do the same to get a dynamic Ruby client to the REST API.
I saw a list of tools in the OS integrations Swagger page but most of them seems to be to automate tests using Swagger or to create a Swagger/openapi API, not to create Client that consume a Swagger API.
Svelte, is a "Dynamic Ruby API Client from Swagger JSON Spec" in the above list. It could be a good candidate, and looks similar to the Bravado Python lib I already use, but:
Here is code in Python that is the kind of feature we are looking for in Ruby:
To get simple dict answer (without using models):
from bravado.client import SwaggerClient
from bravado.fido_client import FidoClient
client = SwaggerClient.from_url(
'http://petstore.swagger.io/v2/swagger.json',
config={'use_models': False}
)
result = client.pet.getPetById(petId=42).result(timeout=4)
provide:
>>> result
{'category': {'id': 42, 'name': 'string'},
'id': 42,
'name': 'doggie',
'photoUrls': ['string', 'string2'],
'status': 'available',
'tags': [{'id': 42, 'name': 'string'}]}
And even better, by default using the model:
> from bravado.client import SwaggerClient
> client = SwaggerClient.from_url("http://petstore.swagger.io/v2/swagger.json")
> pet = client.pet.getPetById(petId=42).result()
> print(pet)
Pet(category=Category(id=42, name='string'), id=42,
name='doggie', photoUrls=['string', 'string2'],
status='available',
tags=[Tag(id=42, name='string')])
>
OpenAPI and Swagger used to refer to the same thing. While there are differences today (OpenAPI refers to RESTful API design and Swagger refers to a set of SmartBear tools), this blog will use the terms interchangeably. If you develop software today, chances are you are developing web APIs as well.
Swagger is a Web API specification document that helps developers design, build, document, and consume RESTfulweb services. Swagger tools support automated documentation, code generation, and test-case generation.
You can use ruby-swagger to convert swagger.json to API client
You can look at this command:
rake swagger:generate_client:ruby
Also you can look at swagger-codegen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With