Coder Social home page Coder Social logo

kapi's Introduction

Rest API server (spec)

Entities

Users

Feeds

Posts

UserPosts

UserFeeds

Queries

Tags

Meta attributes (shared by all entities)

_type : the type of resource

_id : a unique identifier for a resource

_href : the URL of the current resource

_links : an array of relationship URLs

  • each link is an object containing rel and href fields
{
  "rel": "user",
  "href": "http://example.com/api/users/a123"
}

Supported content types

  • Content-Type: application/json; charset=utf-8

Response status codes

Success

Status CodeStatusUsed WithNotes
200OKPUT,PATCH
201CreatedPOST
202AcceptedAsync requests
204No ContentDELETE
204No ContentOPTIONS
304Not ModifiedGETwith ETag header

Failure

  • all error codes should be accompanied by an error payload that explains the error
{
  "message": "error reason",
  "documentationUrl": "http://api/documentation"
}
Status CodeStatusDescription
400Bad Requestthe request was invalid or cannot be served
401Unauthorized
403Forbiddenthe server understood the request but is refusing it
404Not Found
405Method Not Allowedthe requested URL exists but the HTTP method is not applicable
406Not Acceptablethe request has accept headers incompatible with what the server is able to generate
409Conflictsignals a duplicate entries attempt
422Unprocessable Entitythe server cannot process the entity, entity is invalid
415Unsupported Media Typethe accepted media type is not supported

Per HTTP method

MethodActionCollection (/users)Resource (/users/3)
POSTCreate201405, 409
GETRead200200, 404 (invalid id)
PUTReplace405200, 204, 404
PATCHModify405200, 204, 404
DELETEDelete405200, 204, 404

Headers

HeaderReturned On
AllowGET,PUT,…405 response
Locationresource-urlcreation
Linkpaginationcollection query
X-Total-Counttotal count during paginationcollection query

Examples

Query and filtering

Via query string parameters

  • GET /users?name=joe&createdAt=123
  • GET /users?where=(name eq “joe”) and (createdAt lt “123”)
  • GET /users?where=profile.phone eq “112-332-4423”
Filtering operators
OperatorDescriptionExample
eqequalname eq ‘Tom’
nenot equalname ne “Tom”
gtgreater thanheight gt 6
gegreater than or equalversion ge “1.0”
ltless than
leless than or equal
inin listid in [1,2,3]
not innot in listid not in [1,2,3]
containscontainsname contains “the”
is nullis nullversion is null
is not nullis not nulldescription is not null
andcombine two conditions via AND(active eq true) and (version ge “1.1”)
orcombine two conditions via OR(active eq false) or (version ge “1.1”)
Supported types
TypeSample Query
Integerprice gt 123
Floatprice lt 2.5
Stringname eq “Tom”surrounded by double quotes
Date (UTC)startDate gt 2017-01-05T05:27:03.213ZISO 8601
Booleanadmin eq true

Via query object for custom queries

  • objects that support queries should be query-able via a query id like so ?q=<query-id>

Fields

  • all fields used in query, filtering, sorting, can target nested entities (e.g. user.profile)

Sorting

  • sort is allowed over multiple fields, -desc, +asc (default) by using the sort query parameter
  • GET /users?sort=-createdAt,+name,+profile.address

Partial response

  • by default the server returns a full entity with all the nested entities
  • a subset of fields can be requested when necessary by using the include query parameter
  • one or more fields can be excluded using the exclude query parameter
  • only an include or an exclude parameter is allowed
  • a request containing both an include and an exclude parameter will result in a 400 error
  • GET /users?include=createdAt,name,id,profile.address,profile.phone
  • GET /posts?exclude=description

Pagination

  • all collection endpoints will accept start and limit parameters
  • GET /users?start=3&limit=10

Validation

Endpoints

Definition

MethodScopeExampleSemantics
GETcollection/usersretrieve all resources in a collection
GETresource/users/3retrieve a single resource
HEADcollection/usersretrieve all resources in a collection (header only)
HEADresource/users/3retrieve a single resource (header only)
POSTcollection/userscreate one or more resources in a collection
PUTcollection/usersreplace one or more resource (missing fields are deleted)
PUTresource/users/3replace a resource (missing fields are deleted)
PATCHcollection/usersupdate one or more resource (missing fields are left untouched)
PATCHresource/users/3update a resource (missing fields are left untouched)
DELETEresources/users/3delete a resource
OPTIONSany/return available HTTP methods and other options

Supported verbs per endpoint

  • there should be an apps:id/.. for every resource except apps
EndpointGETPOSTPUTPATCHDELETEHEADOPTIONSBulk Supported
apps11000110
apps:id10111110
users11000110
users:id10111110
apps:id/users/11000110
apps:id/users/:id10111110
feeds11110111
feeds:id10111110
posts11110111
posts:id10111110
user-posts11111111
user-posts:id10111110
user-feeds11111111
user-feeds:id10111110
users:id/user-feeds11111111
users:id/user-feeds/:id10111110
apps:id/users/:id/user-feeds11111111
apps:id/users/:id/user-feeds/:id10111110
queries11000111
queries:id10111110
apps:id/queries/11000111
apps:id/queries/:id10111110
tags11111111
tags:id10111110
apps:id/tags/11111111
apps:id/tags/:id10111110
users:id/tags11111111
users:id/tags/:id10111110
users:id/queries11111111
users:id/queries/:id10111110
apps:id/users/:id/tags11111111
apps:id/users/:id/tags/:id10111110
apps:id/users/:id/queries11111111
apps:id/users/:id/queries/:id10111110

Inputs and outputs

  • all methods not in the table below will receive a 405 status code with Allow headers
MethodUrlQueryHeadersBody–>StatusHeadersBody
GETusers:idAccept: *200/404User:JSON
Accept: application/json200/404
Accept: other406
ETag/If-None-Match200/304
GET/usersinclude/excludeAccept: *200/400Link[User]:JSON
whereAccept: application/json200/404X-Total-Count
sortAccept: other406
start
limit
DELETEusers:id204/404
POST/usersUser:JSON201/409Location
POST (bulk)/users[User]:JSON200Link
PUTusers:idUser:JSON204
PUT (bulk)/users200
PATCHusers:id204
PATCH (bulk)/users200
HEADANY200ETag
Last-Modified
OPTIONSANY200Allow

Notes

Bulk operations
  • Bulk operations are performed by sending an array of objects at the collection level
  • Some collection resources accept both a single element and a collection for methods such as PUT,POST,PATCH and according to the input payload the server will detect if a single or bulk operation is requested
  • Bulk operations never fail but will return an array of responses (including error and success results)
  • Bulk creations will return a list of Link headers instead of the Location header
  • Bulk creations will return a 200 status instead of 201 since not all requested items may succeed in being created but the operation as a whole will always succeed
Idempotency
  • All GET, PUT, DELETE, HEAD, and OPTIONS requests for single operations are idempotent

Store specific

Elastic-search

  • elastic search documents will be indexed when modified

Resources

http://restcookbook.com

http://restful-api-design.readthedocs.io/en/latest

http://restlet.com/blog/2015/05/18/implementing-bulk-updates-within-restful-services

http://amundsen.com/media-types/collection/format/

http://www.restapitutorial.com/lessons/restquicktips.html

http://www.restapitutorial.com/lessons/httpmethods.html

https://www.targetprocess.com/guide/integrations/rest-api/rest-api-response-paging-sorting-filters-partial-get/

Diagrams

https://raw.githubusercontent.com/wiki/Webmachine/webmachine/images/http-headers-status-v3.png
https://camo.githubusercontent.com/4e15cccf2a9277dcca2c8824092547dee7058744/68747470733a2f2f7261776769746875622e636f6d2f666f722d4745542f687474702d6465636973696f6e2d6469616772616d2f6d61737465722f6874747064642e706e67

Rest API server (implementation)

kapi's People

Contributors

gabesoft avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.