API

RELATED: EXAMPLES > Cloud Solutions#API |

Public APIs

List to be sorted...

MISC

WEATHER

Examples

RELATED: EXAMPLES > Cloud Solutions#API |

Basic API; from json file source; graphql

Github: https://github.com/gabepublic/api-graphql-js-file-01

Summary:

  • Language(s): javascript

  • Frameworks: graphql

  • API source: json file

Simple 5 countries API; from json file; python flask; docker

Github: https://github.com/gabepublic/docker-api-countries-pyflask

Summary:

  • Language(s): python flask

  • API source: json file

Source: List of Countries [GraphQL] - https://github.com/trevorblades/countries

OpenAPI definition

  • Excellent Reference: Designing APIs with Swagger and OpenAPI by Josh Ponelat, Lukas Rosenstock; Published by Manning Publications Web APIs

Swagger Editor

Web Editor: https://editor.swagger.io/

  • Use the swagger editor to develop OpenAPI definitions

  • Validate the OpenAPI specifications

Mock Server

Choices:

Setup Prism:

  • Prerequisite: Node.js v17+

  • Install

$ npm install --global @stoplight/prism-cli
[...]
$ prism --help
  • Start the mock server

$ prism mock -p 8080 ./openapi.yml
[CLI] ...  awaiting  Starting Prism...
  • Validate from another console

$ curl http://localhost:8080/jobs

Implementation

Codegen

Codegen takes an OpenAPI file then generates code in various programming languages. Two types: for client, and for server. Codegen also generates documentation.

Tools:

Python

Framework comparison:

  • Django-REST - tenure and track record as a framework that can solve a wide variety of business application problems makes it a good choice for complex, long-lived applications to be maintained by a large developer team.

  • Fastapi - stands out with its built-in asynchrony and an API documentation endpoint. Its maintainer-written documentation makes up quite a bit for the lack of organic documentation that comes with its younger age. Its use case is strictly APIs, and it does that one thing remarkably well.

  • Flask - simplicity and wealth of documentation make it an excellent choice for lightweight APIs. Especially APIs where the logic is the focus rather than the versatility of the API itself. See Example above > (Simple 5 countries API; from json file; python flask; docker)

Django-REST

Example TBD

fastapi

GraphQL

AWS

Testing

Tools

curL

$ curl -v http://www.example.com/
$ curl -o out.json http://www.example.com/index.html
$ curl -d 'id=9&name=baeldung' http://localhost:8082/spring-rest/foos/new
$ curl -d @request.json -H "Content-Type: application/json" http://localhost:8082/spring-rest/foos/new
$ curl -d '{"id":9,"name":"baeldung"}' -H 'Content-Type: application/json' http://localhost:8082/spring-rest/foos/new
$ curl -d @request.json -H 'Content-Type: application/json' -X PUT http://localhost:8082/spring-rest/foos/9
$ curl -X DELETE http://localhost:8082/spring-rest/foos/9
$ curl --user baeldung:secretPassword http://example.com/

HTTP request on TCP connection

OS: Linux Commands: telnet, openssl Tested: ubuntu

Instructions
  • Open a TCP connection (http or https)

$ telnet apis.public.com 80
# Or for HTTPS sites...
$ openssl s_client -quiet -connect api.minimalavatars.com:443
  • Send GET /v1/reviews over TCP


GET /v1/reviews HTTP/1.1 <enter>          ❶
Host: farmstall.designapis.com <enter>    ❷
<enter>  ❸

❶ This is the status line, which includes the method, URI, and version of HTTP protocol. ❷ The host header is important because a lot of servers host multiple sites and use the host header to determine which site you are asking for. ❸ A blank line separates the headers from the body section.

  • Example of openssl connection with a response

$ openssl s_client -quiet -connect \
   farmstall.designapis.com:443                     ❶
depth=0 CN = letsencrypt-nginx-proxy-companion      ❷
verify error:num=18:self signed certificate
verify return:1
depth=0 CN = letsencrypt-nginx-proxy-companion
verify return:1
GET /v1/reviews HTTP/1.1                            ❸
Host: farmstall.designapis.com
 
HTTP/1.1 200 OK                                     ❹
Server: nginx/1.17.5
Date: Thu, 14 Nov 2019 09:24:50 GMT
Content-Type: application/json
Content-Length: 465
Connection: keep-alive
Vary: Origin
X-Ratelimit-Limit: 36
X-Ratelimit-Remaining: 35
X-Ratelimit-Reset: 1573723550
 
[{"uuid":"16f5e7e1-b581-4ca4-8af2-8dead5894869","message":"Was okay.",
"rating":3,"userId":""},{"uuid":"92da1efe-a0ab-40a5-bbb9-466e7c32e96d",
"message":"Was terrible.","rating":1,"userId":""},{"uuid":
"5ca80db6-82f7-41a6-8c54-19fb7db77a31", "message":"hello","rating":5,
"userId":""},{"uuid":"13151e0e-f3e7-4f33-ad5b-d4bda9adf496","message":
"hello", "rating":5,"userId":""},{"uuid":
"e4d99a5c-5883-43e7-8133-bb05bf34d0d9","message":"Was awesome!","rating":5,
"userId":""}]                                       ❺

❶ The openssl command to open up the connection ❷ Some connection details (not typed) ❸ Typing out the HTTP request ❹ The start of the response (not typed) ❺ The response body (not typed)

  • POST over TCP

POST /v1/reviews HTTP/1.1 <enter>                  ❶
Host: farmstall.designapis.com <enter>
Content-Length: 37 <enter>                         ❷
Content-Type: application/json <enter>             ❸
<enter>                                            ❹
{"message": "neckbeard", "rating": 5} <enter>      ❺

❶ Use the POST method. ❷ Indicate the size of the body (we counted it for you). ❸ Specify the media type of the payload. ❹ Add a blank line to separate the header section from the body. ❺ Enter the body (all 37 characters in this example).

As soon as you hit that last, you should get a response. Note: if you increase the Content-Length to a larger value, your response will only be returned after you press Enter multiple times.

Last updated