Skip to content

Commit

Permalink
Add http-location helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth committed Aug 9, 2015
1 parent 55e1fd6 commit 2a67b6d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions request/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private/base.rkt"
"private/struct.rkt"
"private/exn.rkt"
"private/http-location.rkt"
"private/call-response.rkt"
"private/wrap.rkt")

Expand All @@ -15,6 +16,7 @@
"private/base.rkt"
"private/struct.rkt"
"private/exn.rkt"
"private/http-location.rkt"
"private/wrap.rkt")
requester-get
requester-put
Expand Down
1 change: 1 addition & 0 deletions request/main.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ source code: @url["https://github.com/jackfirth/racket-request"]
@include-section["private/wrap.scrbl"]
@include-section["private/base.scrbl"]
@include-section["private/exn.scrbl"]
@include-section["private/http-location.scrbl"]
@include-section["param.scrbl"]
@include-section["check.scrbl"]
24 changes: 24 additions & 0 deletions request/private/http-location.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#lang racket

(require net/url
fancy-app
"struct.rkt"
"wrap.rkt")

(provide
(contract-out
[make-domain-requester (-> string? requester? requester?)]
[make-host+port-requester (-> string? exact-nonnegative-integer? requester? requester?)]))


(define (domain+relative-path->http-url domain relative-path)
(string->url (format "http://~a/~a" domain relative-path)))

(define (host+port->domain host port)
(format "~a:~a" host port))

(define (make-domain-requester domain requester)
(wrap-requester-location (domain+relative-path->http-url domain _) requester))

(define (make-host+port-requester host port requester)
(make-domain-requester (host+port->domain host port) requester))
41 changes: 41 additions & 0 deletions request/private/http-location.scrbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#lang scribble/manual

@(require "../doc-utils/examples.rkt"
"../doc-utils/def.rkt"
(for-label request
net/url
racket))

@title{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.

@defproc[(make-domain-requester [domain string?]
[requester requester?])
requester?]{
Given a requester that accepts @racket[url?]s
as locations, returns a requester that accepts
@racket[string]s representing relative paths as
locations. Each path is combined with the given
@racket[domain] to construct a full http @racket[url],
which is then passed to the underlying @racket[requester].
The relative path should not begin with a slash.
@racketinput[
(define foo-com-requester
(make-domain-requester "foo.com" http-requester))
(get foo-com-requester "some/sort/of/path") ;; request to http://foo.com/some/sort/of/path
]}

@defproc[(make-host+port-requester [host string?]
[port exact-nonnegative-integer?]
[requester requester?])
requester?]{
Like @racket[make-domain-requester], except combines
the @racket[host] and @racket[port] into a domain string.
@racket[(make-host+port-requester "foo.com" 8080 some-requester)]
is equivalent to @racket[(make-domain-requester "foo.com:8080" some-requester)]
}

0 comments on commit 2a67b6d

Please sign in to comment.