Requests
(require request) | package: request |
This library includes functions and forms for working with requests and requesters. A request is either a GET request, a PUT request, a POST request, or a DELETE request. A requester is a value that can be used to perform these types of requests. This library provides several requesters built on top of the HTTP protocol, however in principle these functions work with any requester that can be constructed to perform each of the types of requests.
source code: https://github.com/jackfirth/racket-request
1 Requesters
procedure
(requester get put post delete) → requester?
get : (->* (any/c) (#:headers list?) any/c) put : (->* (any/c any/c) (#:headers list?) any/c) post : (->* (any/c any/c) (#:headers list?) any/c) delete : (->* (any/c) (#:headers list?) any/c)
GET - Given a location, returns a response. Should be safe - calling GET on a location should never modify the resource at the location, and the GET should be invisible to anyone else viewing or modifying that resource.
PUT - Given a location and a body, returns a response. Should be idempotent - doing a PUT twice at the same location with the same body should be exactly the same as doing it once. Additionally, for a location that can be PUT to, a GET response should contain what was last PUT there.
POST - Given a location and a body, returns a response. A post need not be either safe or idempotent, it may perform arbitrary modification of the resource at the location or other resources related to that resource. A location that is POST-ed to should contain a resource - that is, a GET at that location should not return a resource not found response.
DELETE - Given a location, returns a response. In the event of a successful response, later GETs (and DELETEs) at that location should be unsuccessful.
value
requester? : predicate/c
procedure
requester : requester? location : any/c headers : list? = '()
procedure
(put requester location body [ #:headers headers]) → any/c requester : requester? location : any/c body : any/c headers : list? = '()
procedure
(post requester location body [ #:headers headers]) → any/c requester : requester? location : any/c body : any/c headers : list? = '()
procedure
(delete requester location body [ #:headers headers]) → any/c requester : requester? location : any/c body : any/c headers : list? = '()
2 Extending and Wrapping Requesters
procedure
(wrap-requester wrapper requester) → requester?
wrapper :
(or/c (-> (->* (any/c) (#:headers list?) any/c) (->* (any/c) (#:headers list?) any/c)) (-> (->* (any/c any/c) (#:headers list?) any/c) (->* (any/c any/c) (#:headers list?) any/c))) requester : requester?
procedure
(wrap-requester-location location-wrapper requester) → requester? location-wrapper : (-> any/c any/c) requester : requester?
procedure
(wrap-requester-body body-wrapper requester) → requester? body-wrapper : (-> any/c any/c) requester : requester?
procedure
(wrap-requester-response response-wrapper requester) → requester? response-wrapper : (-> any/c any/c) requester : requester?
procedure
(add-requester-headers headers requester) → requester?
headers : list? requester : requester?
3 HTTP Requests and Requester
value
http-requester : requester?
struct
(struct http-response (code headers body))
code : exact-positive-integer?
headers :
(hash/c string? string? #:immutable? #t) body : string?
4 HTTP Status Code Exception Throwing
struct
code : exact-positive-integer?
procedure
(requester-http-exn requester) → requester?
requester : requester?
value
http-requester/exn : requester?
procedure
(http-exn-of-code? code v) → boolean?
code : exact-positive-integer? v : any/c
5 HTTP Requester Location Wrappers
Usually an http requester is constructed for a single REST API at a particular domain. These functions allow the construction of requesters that operate at only one domain and accept relative paths as locations.
procedure
(make-domain-requester domain requester) → requester?
domain : string? requester : requester?
> (define foo-com-requester (make-domain-requester "foo.com" http-requester)) (get foo-com-requester "some/sort/of/path")
procedure
(make-host+port-requester host port requester) → requester? host : string? port : exact-nonnegative-integer? requester : requester?
6 Parameterized Requests
(require request/param) | package: request |
When using requesters, it is usually the case that first a requester is constructed, then all requests are made with it. This makes specifying the requester in each request verbose and redundant. This module provides request functions that operate using a current-requester parameter which can be modified using with-requester.
parameter
(current-requester requester) → void? requester : requester?
= http-requester
syntax
(with-requester requester-expr body ...)
requester-expr : requester?
procedure
location : any/c headers : list? = '()
procedure
location : any/c body : any/c headers : list? = '()
procedure
location : any/c body : any/c headers : list? = '()
procedure
location : any/c headers : list? = '()
7 RackUnit Requester Integration Testing
(require request/check) | package: request |
This module provides rackunit checks that test requests using the current-requester from request/param. This can be used as a lightweight HTTP API integration testing framework. Note that none of these checks accept headers. Use with-requester and add-requester-headers to add headers to the current requester for a set of checks. This module also re-provides everything in request/param.
procedure
(check-put location body expected-response) → void?
location : any/c body : any/c expected-response : any/c
procedure
(check-post location body expected-response) → void?
location : any/c body : any/c expected-response : any/c
procedure
(check-delete location expected-response) → void?
location : any/c expected-response : any/c
procedure
(check-get-exn exn-pred location) → void?
exn-pred : predicate/c location : any/c
procedure
(check-put-exn exn-pred location body) → void?
exn-pred : predicate/c location : any/c body : any/c
procedure
(check-post-exn exn-pred location body) → void?
exn-pred : predicate/c location : any/c body : any/c
procedure
(check-delete-exn exn-pred location) → void?
exn-pred : predicate/c location : any/c