specgen
  • Introduction
  • spec
    • Format Overview
    • Types
    • Models
    • HTTP Endpoints
  • specgen
    • Overview
    • Specgen Reference
    • Plugins
      • gradle
      • Maven
      • npm
      • Sbt
      • RAKE
Powered by GitBook
On this page
  • HTTP Endpoints
  • API
  • Endpoint
  • Endpoint Field
  • Parameters
  • Header Parameters
  • Query Parameters
  • Request Body
  • Response
  1. spec

HTTP Endpoints

PreviousModelsNextOverview

Last updated 3 years ago

HTTP Endpoints

Here's an example of HTTP endpoint definition:

http:                        #  HTTP Endpoints
  sample:                    #  API
    get_sample:              #  Endpoint
      endpoint: GET /sample
      response:
        ok: Sample

The http field of the spec is a dictionary where each item represents an .

API

API is a logical grouping of endpoints. This grouping is important for code generation as it allows to split code into logically separate files/classes.

Each key in the http dictionary is a name of the API and value is a dictionary defining belonging to the API.

Here's an example of two APIs books and authors with two endpoints in each.

http:
  books:                       # Books API
    all_books:
      endpoint: GET /books
      response:
        ok: Books
    add_book:
      endpoint: POST /books
      body: Book
      response:
        ok: empty
  authors:                     # Authors API
    all_authors:
      endpoint: GET /authors
      response:
        ok: Authors
    add_author:
      endpoint: POST /authors
      body: Author
      response:
        ok: empty

Endpoint

Here's an example of endpoint definition:

create_sample:
  description: creates sample
  endpoint: POST /sample
  header:
    Authorization: string
  query:
    sample_id: uuid
    user_id: int?
  body: Sample
  response:
    ok: Sample
    forbidden: empty

Here's information about endpoint definition fields:

Field
Required
Details

description

no

description, used for documentation

yes

HTTP method and endpoint URL

no

dictionary of HTTP header parameters and their types

no

dictionary of HTTP query parameters and their types

no for POST and PUT

HTTP body of the request

yes

dictionary of supported HTTP responses and response body

Endpoint Field

Endpoint defines HTTP method and URL to which service should respond.

Endpoint has the following format: METHOD url. Method might be one of follwing: GET, POST, PUT, DELETE.

Url is just a string always starting from /. Url can contain parameters in following format: {param:type}. Here's an example for enpoint with url parameter:

GET /sample/{id:uuid}

Parameters

parameter: type [= default_value] [# description]
Required
Details

parameter

yes

parameter name

type

yes

default_value

no

default value for the parameter

description

no

description in a form of line comment, used for documentation

Here's an example of parameter page_size of type int default value 20 and description number of items per page:

page_size: int = 20  # number of items per page

Here's an example of parameter page of type int without default value or description:

page: int

Header Parameters

Here's an example of two header parameters definition: Authorization (string) and X-Request-Id (uuid):

header:
  Authorization: string  # authorization token
  X-Request-Id: uuid     # original request id passed

Omit header field in endpoint defintion if there no header parameters needed.

Query Parameters

Here's an example of two query parameters definition: page_size (int) and page_number (int):

query:
  page_size: int = 100  # size of the page
  page_number: int      # number of requested page

Omit query field in endpoint defintion if there no query parameters needed.

Request Body

Here's an example of body definition represented by custom type Sample:

body: Sample  # sample that will be created

The body field can not have default value. Omit body field in endpoint definition if request body is not required.

Response

Here's an example of response definition with 2 responses: ok and forbidden, the ok response has a body of type Sample along with descriptions:

response:
  ok: Sample        # sample is created
  forbidden: empty  # user is forbidden to create sample

At least one response should be always defined for any HTTP endpoint.

This section describes parameters format that is used for both and .

type of parameter according to

The header field of allows to define HTTP header parameters. The header field value is a dictionary where each item is using format.

The query field of allows to define HTTP header parameters. The query field value is a dictionary where each item is using format.

The body field of defines body type of HTTP endpoint request.

The response field of defines all possible responses of the HTTP endpointed. The response is a dictionary where key is the name of the response and value is the type of the response. Responses names should be in a text form according to but in snake_case. So, OK will be ok, Unauthorized - unauthorized, Method Not Allowed - method_not_allowed, etc.

API
endpoints
header parameters
query parameters
endpoint
parameters
endpoint
parameters
endpoint
RFC 7231
endpoint
types
endpoint
header
query
body
response