Writing our specs

We won't be able to introduce the full rules for writing API specs here, and also won't be able to include all of its features in our example. Furthermore, a complete description for any API can be hundreds of lines long, and that's another problem. So, let's just go over some basic definitions, as well as a couple of the services, to get a taste of what needs to be done. First, we'll need some basic data about our server:

swagger: "2.0"
info:
description: "This is a RESTful API to access countries, regions, and cities."
version: "1.0.0"
title: "World Data API"

host: "127.0.0.1:8443"
schemes:
- "http"

Then we must describe the tags (think sections) that our documentation will be divided into. We work with tokens (for security), plus countries, regions, and cities, so those seem to be the needed definitions:

tags:
- name: "token"
description: "Get a JWT for authorization"
- name: "countries"
description: "Access the world countries"
- name: "regions"
description: "Access the regions of countries"
- name: "cities"
description: "Access the world cities"

Let's take a look at the /gettoken route. We define a POST request, which gets body encoded parameters, and returns plain text. Two string parameters, user and password, are required. The API may either return a 200 status if everything was OK, or 401 otherwise:

paths:
/gettoken:
post:
tags:
- "token"
summary: "Get a token to authorize future requests"
consumes:
- "application/x-www-form-urlencoded"
produces:
- text/plain
parameters:
- in: formData
name: user
required: true
type: string
- in: formData
name: password
required: true
type: string
responses:
200:
description: A valid token to use for other requests
401:
description: "Wrong user/password"

Getting regions for a country would get a similar specification:

/regions:
get:
tags:
- "regions"
summary: "Get all regions of all countries"
produces:
- application/json
parameters:
- in: header
name: "Authorization"
required: true
type: string
description: Authorization Token
responses:
200:
description: "OK"
401:
description: "No token provided"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.56.45