Skip to content

Commit

Permalink
Rename response struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth committed Jul 13, 2015
1 parent 7bd1b8e commit 48c7253
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion request/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
requester-post
requester-delete
struct:requester)
(struct-out response))
(struct-out http-response))
38 changes: 24 additions & 14 deletions request/private/base.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,45 @@

@title{HTTP Requests and Requester}

@defrequester[base-requester]{
@defmodule[request]

@defrequester[http-requester]{
A simple requester for the HTTP protocol built with
@racket[get-impure-port], @racket[put-impure-port],
@racket[post-impure-port], and @racket[delete-impure-port].
Locations are @racket[url?]s, headers are @racket[string?]s
as in the impure port functions, bodies are @racket[bytes?],
and responses are instances of the @racket[response] struct.
and responses are instances of the @racket[http-response] struct.
}

@defstruct*[response ([code exact-positive-integer?]
[headers (hash/c string? string?
#:immutable? #t)]
[body string?])]{
@defstruct*[http-response ([code exact-positive-integer?]
[headers (hash/c string? string?
#:immutable? #t)]
[body string?])]{
A structure type for HTTP responses. Contains a status
code, a hash of headers, and a raw body string.
@racket[base-requester] responds with instances of
this structure type.
@racket[http-requester] responds with instances of
this structure type. This is distinct from the
@racket[response] structure type in the web server
package, as that response is for @italic{sending}
responses while this struct is used when
@italic{receiving} them.
}

@defpredicate[response?]{Predicate satisfied by @racket[response]s.}
@defstructinfo[struct:response]{Struct info instance for @racket[response]s.}
@defpredicate[http-response?]{
Predicate satisfied by @racket[http-response]s.
}
@defstructinfo[struct:http-response]{
Struct info instance for @racket[http-response]s.
}

@deftogether[(
@defproc[(repsonse-code [response response?])
@defproc[(http-response-code [http-response http-response?])
exact-positive-integer?]
@defproc[(response-headers [response response?])
@defproc[(http-response-headers [http-response http-response?])
(hash/c string? string?
#:immutable? #t)]
@defproc[(response-body [response response?])
@defproc[(http-response-body [http-response http-response?])
string?])]{
Field accessors for instances of the @racket[response] struct.
Field accessors for instances of the @racket[http-response] struct.
}
14 changes: 7 additions & 7 deletions request/private/call-response.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
typed/net/head
fancy-app)

(provide (struct-out response)
Response
(provide (struct-out http-response)
HttpResponse
Url
call-response/input-url)


(struct response
(struct http-response
([code : Positive-Integer]
[headers : (HashTable String String)]
[body : String]) #:transparent)

(define-type Response response)
(define-type HttpResponse http-response)
(define-type Url url)

(: not-newline? (-> Char Boolean))
Expand All @@ -41,17 +41,17 @@
(define code-chars (takef dropped-protocol not-whitespace?))
(cast (string->number (apply string code-chars)) Positive-Integer))

(: impure-port->response (-> Input-Port Response))
(: impure-port->response (-> Input-Port HttpResponse))
(define (impure-port->response impure-port)
(define HTTP-header+MIME-headers (purify-port impure-port))
(define-values (HTTP-header MIME-headers)
(split-combined-header HTTP-header+MIME-headers))
(define status-code (http-header-code HTTP-header))
(define headers (cast (make-hash (extract-all-fields MIME-headers)) (HashTable String String)))
(define raw-body (port->string impure-port))
(response status-code headers raw-body))
(http-response status-code headers raw-body))


(: call-response/input-url (-> Url (-> Url Input-Port) Response))
(: call-response/input-url (-> Url (-> Url Input-Port) HttpResponse))
(define (call-response/input-url url connect)
(call/input-url url connect impure-port->response))

0 comments on commit 48c7253

Please sign in to comment.