Swagger implementation

Basically, we will split the Swagger we created in Chapter 3Designing RESTful APIs with OpenAPI and Swagger, into two new ones with fewer operations, since there is no need to log in anymore.

At the time of writing, Google Cloud Endpoints supports only Swagger Version 2: https://cloud.google.com/endpoints/docs/openapi/.

The order-ms Swagger will be as follows:

# [START swagger]
swagger: '2.0'
info:
description: Order Microservice
title: Order Microservice
version: 1.0.0
host: order-ms-227100.appspot.com
# [END swagger]
consumes:
- application/json
produces:
- application/json
- application/xml
schemes:
- https
- http
paths:
/api:
get:
tags:
- api
description: Returns a default api message
operationId: getApi
security:
- api_key: []
produces:
- application/json
responses:
200:
description: successful operation
/store/inventory:
get:
tags:
- store
summary: Returns user inventories from the store
description: Returns a map of status codes to quantities
operationId: getInventory
responses:
200:
description: successful operation
security:
- api_key: []
/store/orders:
post:
tags:
- store
summary: Place an order for a user
operationId: addOrder
produces:
- 'application/json'
responses:
201:
description: successful operation
schema:
$ref: '#/definitions/Order'
400:
description: Invalid Order
security:
- api_key: []
parameters:
- description: Order information
in: body
name: message
required: true
schema:
$ref: '#/definitions/Order'
/store/orders/{orderId}:
get:
tags:
- store
summary: Find purchase order by ID
operationId: getOrder
produces:
- 'application/json'
- 'application/xml'
responses:
201:
description: successful operation
schema:
$ref: '#/definitions/Order'
400:
description: Invalid ID supplied
404:
description: Order not found
security:
- api_key: []
parameters:
- in: path
name: orderId
type: integer
required: true
description: Numeric ID of the order to get.
delete:
tags:
- store
summary: Deletes the order with the specified ID.
operationId: removeOrder
responses:
204:
description: User was deleted.
400:
description: Invalid ID supplied
404:
description: Order not found
security:
- api_key: []
parameters:
- in: path
name: orderId
type: integer
required: true
description: Numeric ID of the order to get.
definitions:
Order:
type: object
properties:
id:
type: integer
format: int64
userId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
# This section requires all requests to any path to require an API key.
security:
- api_key: []
securityDefinitions:
api_key:
type: 'apiKey'
name: 'key'
in: 'query'

Pay attention to the host parameter, since you have to specify the host based on the project ID you created for order-ms:

host: order-ms-227100.appspot.com

The following screenshot shows Swagger visualization of Order Microservice:

The rder microservice Swagger visualization

We will do the same thing with the user-ms swagger:

# [START swagger]
swagger: '2.0'
info:
description: User Microservice
title: User Microservice
version: 1.0.0
host: user-ms-227100.appspot.com
# [END swagger]
consumes:
- application/json
produces:
- application/json
- application/xml
schemes:
- https
- http
paths:
/api:
get:
tags:
- api
description: Returns a default api message
operationId: getApi
security:
- api_key: []
produces:
- application/json
responses:
200:
description: successful operation
/users:
post:
tags:
- store
summary: Create a user
operationId: addUser
produces:
- 'application/json'
responses:
201:
description: successful operation
schema:
$ref: '#/definitions/User'
400:
description: Invalid User
security:
- api_key: []
parameters:
- description: User information
in: body
name: message
required: true
schema:
$ref: '#/definitions/User'
/users/{username}:
get:
tags:
- store
summary: Find user by username
operationId: getUser
produces:
- 'application/json'
- 'application/xml'
responses:
201:
description: successful operation
schema:
$ref: '#/definitions/User'
400:
description: Invalid ID supplied
404:
description: User not found
security:
- api_key: []
parameters:
- in: path
name: username
type: string
required: true
description: username of the user to get.
patch:
tags:
- store
summary: Update the user with the specified username.
operationId: updateUser
responses:
204:
description: User was updated.
400:
description: Invalid ID supplied
404:
description: User not found
security:
- api_key: []
parameters:
- in: path
name: username
type: string
required: true
description: username of the user to update.
- in: body
name: message
required: true
schema:
$ref: '#/definitions/User'
delete:
tags:
- store
summary: Deletes the user with the specified username.
operationId: removeUser
responses:
204:
description: User was deleted.
400:
description: Invalid ID supplied
404:
description: User not found
security:
- api_key: []
parameters:
- in: path
name: username
type: string
required: true
description: username of the user to delete.
definitions:
User:
type: object
properties:
_id:
type: integer
format: int64
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
password:
type: string
phone:
type: string
userStatus:
type: integer
format: int32
description: User Status
xml:
name: User
# This section requires all requests to any path to require an API key.
security:
- api_key: []
securityDefinitions:
api_key:
type: 'apiKey'
name: 'key'
in: 'query'

As with order-ms, be careful with the host information for user-ms:

host: user-ms-227100.appspot.com

The following screenshot shows Swagger visualization of User Microservice:

The user microservice Swagger visualization

As was mentioned before, it is not necessary to keep the operations neither login or logout since the API key is going to handle that for us from Google Cloud Endpoints.

..................Content has been hidden....................

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