Overview

HTTP verbs

Our application tries to adhere as closely as possible to standard HTTP and REST conventions in its use of HTTP verbs.

Verb Usage

GET

Used to retrieve a resource

POST

Used to create a new resource

PATCH

Used to update an existing resource, including partial updates

DELETE

Used to delete an existing resource

HTTP status codes

Our application tries to adhere as closely as possible to standard HTTP and REST conventions in its use of HTTP status codes.

Status code Usage

200 OK

The request completed successfully

201 Created

A new resource has been created successfully. The resource’s URI is available from the response’s Location header

202 Accepted

An update to an existing resource has been accepted, and will be processed asynchronously. For example an event was accepted by the state machine

204 No Content

An update to an existing resource has been applied successfully

400 Bad Request

The request was malformed. The response body will include an error providing further information

422 Unprocessable Entity

An update to an existing resource has been rejected for semantic reasons. For example an event has not been processed by the state machine.

404 Not Found

The requested resource did not exist

Headers

Every response has the following header(s):

Name Description

Content-Type

The Content-Type of the payload, e.g. application/hal+json

Errors

Whenever an error response (status code >= 400) is returned, the body will contain a JSON object that describes the problem. The error object has the following structure:

Path Type Description

error

String

The HTTP error that occurred, e.g. Bad Request

message

String

A description of the cause of the error

path

String

The path to which the request was made

status

Number

The HTTP status code, e.g. 400

timestamp

String

The time, in milliseconds, at which the error occurred

For example, a request that attempts to create an order with invalid JSON will produce a 400 Bad Request response:

HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Content-Length: 163

{
  "timestamp" : "2018-09-18T16:27:48.889+0000",
  "status" : 400,
  "error" : "Bad Request",
  "message" : "The order cannot be created.",
  "path" : "/orders"
}

Hypermedia

Our application uses hypermedia and resources include links to other resources in their responses. Responses are in Hypertext Application Language (HAL) format. Links can be found beneath the _links key. Users of the API should not create URIs themselves, instead they should use the above-described links to navigate from resource to resource.

Resources

Index

The index provides the entry point into the service.

Accessing the index

A GET request is used to access the index

Response structure

Path Type Description

_links

Object

Links to other resources

Example response

HTTP/1.1 200 OK
Content-Type: application/hal+json;charset=UTF-8
Content-Length: 206

{
  "_links" : {
    "orders" : {
      "href" : "http://localhost:8080/orders{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile"
    }
  }
}
Relation Description

orders

The Orders resource

profile

The ALPS profile for the service

Orders

The Orders resource is used to create and list orders.

Listing orders

A GET request will list all orders.

Response structure

Path Type Description

_embedded.orders

Array

Embedded list of Order resources

page

Object

paging information

Example request

$ curl 'http://localhost:57214/orders' -i -X GET

Example response

HTTP/1.1 200 OK
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 18 Sep 2018 16:27:54 GMT
Content-Length: 1452

{
  "_embedded" : {
    "orders" : [ {
      "currentState" : "Canceled",
      "_links" : {
        "self" : {
          "href" : "http://localhost:57214/orders/2"
        },
        "order" : {
          "href" : "http://localhost:57214/orders/2"
        },
        "reopen" : {
          "href" : "http://localhost:57214/orders/2/receive/Reopen"
        },
        "receive-payment" : {
          "href" : "http://localhost:57214/orders/2/receive/ReceivePayment"
        }
      }
    }, {
      "currentState" : "Open",
      "_links" : {
        "self" : {
          "href" : "http://localhost:57214/orders/3"
        },
        "order" : {
          "href" : "http://localhost:57214/orders/3"
        },
        "receive-payment" : {
          "href" : "http://localhost:57214/orders/3/receive/ReceivePayment"
        },
        "cancel" : {
          "href" : "http://localhost:57214/orders/3/receive/Cancel"
        },
        "unlock-delivery" : {
          "href" : "http://localhost:57214/orders/3/receive/UnlockDelivery"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:57214/orders{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:57214/profile/orders"
    },
    "search" : {
      "href" : "http://localhost:57214/orders/search"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

Creating an order

A POST request is used to create an order

Request structure

Path Type Description

Example request

$ curl 'http://localhost:57214/orders' -i -X POST \
    -H 'Accept: application/hal+json' \
    -H 'Content-Type: application/json; charset=UTF-8' \
    -d '{}'

Example response

HTTP/1.1 201 Created
Location: http://localhost:57214/orders/3
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 18 Sep 2018 16:27:53 GMT
Content-Length: 489

{
  "currentState" : "Open",
  "_links" : {
    "self" : {
      "href" : "http://localhost:57214/orders/3"
    },
    "order" : {
      "href" : "http://localhost:57214/orders/3"
    },
    "receive-payment" : {
      "href" : "http://localhost:57214/orders/3/receive/ReceivePayment"
    },
    "cancel" : {
      "href" : "http://localhost:57214/orders/3/receive/Cancel"
    },
    "unlock-delivery" : {
      "href" : "http://localhost:57214/orders/3/receive/UnlockDelivery"
    }
  }
}

Order

The Order resource is used to navigate to sub-resources triggering order change events.

Relation Description

unlock-delivery

POST: Unlock delivery, to enable fulfillment process even if payment has not been received yet.

deliver

POST: Deliver products

receive-payment

POST: Receive Payment

refund

POST:T Start refund process

reopen

POST: Reopen a closed order

cancel

POST: Cancel an order

Retrieve an order

A GET request will retrieve the details of an order

Response structure

Path Type Description

currentState

String

Current order state. Localized and meant for display purposes only

Example request

$ curl 'http://localhost:57214/orders/2' -i -X GET \
    -H 'Accept: application/hal+json'

Example response

HTTP/1.1 200 OK
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 18 Sep 2018 16:27:53 GMT
Content-Length: 489

{
  "currentState" : "Open",
  "_links" : {
    "self" : {
      "href" : "http://localhost:57214/orders/2"
    },
    "order" : {
      "href" : "http://localhost:57214/orders/2"
    },
    "receive-payment" : {
      "href" : "http://localhost:57214/orders/2/receive/ReceivePayment"
    },
    "cancel" : {
      "href" : "http://localhost:57214/orders/2/receive/Cancel"
    },
    "unlock-delivery" : {
      "href" : "http://localhost:57214/orders/2/receive/UnlockDelivery"
    }
  }
}

Triggering order change events

A POST request to one of the order event sub-resources will trigger a change event for an order. The order resource will only expose links to event sub-resources that are expected to be valid, and the state machine will only process certain events in certain states (see statemachine diagram).

Response structure

The response code will be either be 202 Accepted or 422 Unprocessable Entity, while the response body will always be empty.

Example request

$ curl 'http://localhost:57214/orders/2/receive/ReceivePayment' -i -X POST \
    -H 'Accept: application/hal+json' \
    -H 'Content-Type: application/json; charset=UTF-8'

Example response

HTTP/1.1 202 Accepted
Date: Tue, 18 Sep 2018 16:27:53 GMT