Skip to content

Tutorial1_Basic

incjung edited this page Apr 30, 2018 · 2 revisions

What is RESTful API?

REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP) technology because REST leverages less bandwidth, making it more suitable for internet usage. An API for a website is code that allows two software programs to communicate with each another. The API spells out the proper way for a developer to write a program requesting services from an operating system or other application.

The REST used by browsers can be thought of as the language of the internet. With cloud use on the rise, APIs are emerging to expose web services. REST is a logical choice for building APIs that allow users to connect and interact with cloud services. RESTful APIs are used by such sites as Amazon, Google, LinkedIn and Twitter.

BUT, Service discovery is hard part and required by various use cases. For example a common language to allow software agents to make use of one another's services without the need for continuous user intervention.[1]

Why Swagger?

Swagger is the world’s largest framework of API developer tools for the OpenAPI Specification(OAS), enabling development across the entire API lifecycle, from design and documentation, to test and deployment.

At the heart of the above tools is the OpenAPI Specification (formerly called the Swagger Specification). The specification creates the RESTful contract for your API, detailing all of its resources and operations in a human and machine readable format for easy development, discovery, and integration.

Why cl-swagger-codegen?

There is the swagger codegen project, which allows generation of API client libraries (SDK generation), server stubs automatically given an OpenAPI Spec.

Currently, the following languages/frameworks are supported:

  • API clients: ActionScript, Ada, Apex, Bash, C# (.net 2.0, 3.5 or later), C++ (cpprest, Qt5, Tizen), Clojure, Dart, Elixir, Elm, Eiffel, Erlang, Go, Groovy, Haskell (http-client, Servant), Java (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured), Kotlin, Lua, Node.js (ES5, ES6, AngularJS with Google Closure Compiler annotations) Objective-C, Perl, PHP, PowerShell, Python, R, Ruby, Rust (rust, rust-server), Scala (akka, http4s, swagger-async-httpclient), Swift (2.x, 3.x, 4.x), Typescript (Angular1.x, Angular2.x, Fetch, jQuery, Node) Server stubs: Ada, C# (ASP.NET Core, NancyFx), C++ (Pistache, Restbed), Erlang, Go, Haskell (Servant), Java (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, RestEasy, Play Framework, PKMST), Kotlin, PHP (Lumen, Slim, Silex, Symfony, Zend Expressive), Python (Flask), NodeJS, Ruby (Sinatra, Rails5), Rust (rust-server), Scala (Finch, Lagom, Scalatra)

You can see clojure codegen but CL. So I did.

Try simple swagger example

First of all, you should find the 'swagger.json' which is presenting Swagger Spec. I will use the public swagger.

Please visit http://petstore.swagger.io/

This is a sample server Petstore server. You can find out more about Swagger at http://swagger.io or on irc.freenode.net, #swagger.

To update a pet in the store with form data:

on you colsole

curl -X POST "http://petstore.swagger.io/v2/pet" -H "accept: application/xml" -H "Content-Type: application/json" -d "{ "id": 0, "category": { "id": 0, "name": "string" }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 0, "name": "string" } ], "status": "available"}"

OUTPUT is :

0string11223345524doggiestringavailable0stringincjjung@ijung-M" -H "Content-Type: application/json" -d "{ \"id\": 0, \"category\": { \"id\": 0, \"name\": \"string\" }, \"name\": \"doggie\", \"photoUrls\": [ \"string\" ], \"tags\": [ { \"id\": 0, \"name\": \"string\" } ], \"status\": \"available\"}"

You can see the swagger working.

Start to make a CL swagger client from cl-swagger-codegen

From now, let's make a CL client stub from our cl-swagger-codegen.

(defpackage swagger.test (:use cl oauth2 cl-swagger)) (in-package swagger.test) (generate-client "http://petstore.swagger.io/v2/swagger.json" #p"./example/pet-api-client.lisp")

generate-client will create a client stub for swagger REST apis. Can you fiind the pet-api-client.lisp in the location of ./example/pet-api-client.lisp?

If then, everything is done.

Please load the pet-api-client.lisp. You should find a various of methods to connect the REST APIs.

To add new pet to the online store, use POST-PET. We are using cl-json (https://common-lisp.net/project/cl-json/) to edit json structure.

(post-pet :content (cl-json:encode-json-to-string '((id . 0) (:category . ((:id . 0) (:name . "string"))) (:name . "doggie") ("photoUrls" . #("string")) (:tags . (((:id . 0) (:name . "string")))) (:status . "available"))))

To get a single pet:

(get-pet "/pet/0")

That's it.

Please let me know if you have any issues. Thank you

google oauth2 process

Clone this wiki locally