http.client: adding variants of http-methods that check the response code.
parent
a4f78c9515
commit
18088e1aa3
|
@ -55,41 +55,81 @@ HELP: http-get
|
||||||
{ $description "Downloads the contents of a URL." }
|
{ $description "Downloads the contents of a URL." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-get*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-get } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-post
|
HELP: http-post
|
||||||
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Submits an HTTP POST request." }
|
{ $description "Submits an HTTP POST request." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-post*
|
||||||
|
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-post } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-put
|
HELP: http-put
|
||||||
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Submits an HTTP PUT request." }
|
{ $description "Submits an HTTP PUT request." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-put*
|
||||||
|
{ $values { "post-data" object } { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-put } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-head
|
HELP: http-head
|
||||||
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Same as " { $link http-get } " except that the server is not supposed to return a message-body in the response, as per RFC2616. However in practise, most web servers respond to GET and HEAD method calls with identical responses." }
|
{ $description "Same as " { $link http-get } " except that the server is not supposed to return a message-body in the response, as per RFC2616. However in practise, most web servers respond to GET and HEAD method calls with identical responses." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-head*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-head } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-delete
|
HELP: http-delete
|
||||||
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Requests that the origin server delete the resource identified by the URL." }
|
{ $description "Requests that the origin server delete the resource identified by the URL." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-delete*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-delete } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-options
|
HELP: http-options
|
||||||
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Submits an HTTP OPTIONS request." }
|
{ $description "Submits an HTTP OPTIONS request." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-options*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-options } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-trace
|
HELP: http-trace
|
||||||
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "response" response } { "data" sequence } }
|
||||||
{ $description "Submits an HTTP TRACE request." }
|
{ $description "Submits an HTTP TRACE request." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: http-trace*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "data" sequence } }
|
||||||
|
{ $description "A variant of " { $link http-trace } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: with-http-get
|
HELP: with-http-get
|
||||||
{ $values { "url" "a " { $link url } " or " { $link string } } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "quot" { $quotation "( chunk -- )" } } { "response" response } }
|
||||||
{ $description "Downloads the contents of a URL. Chunks of data are passed to the quotation as they are read." }
|
{ $description "Downloads the contents of a URL. Chunks of data are passed to the quotation as they are read." }
|
||||||
{ $errors "Throws an error if the HTTP request fails." } ;
|
{ $errors "Throws an error if the HTTP request fails." } ;
|
||||||
|
|
||||||
|
HELP: with-http-get*
|
||||||
|
{ $values { "url" "a " { $link url } " or " { $link string } } { "quot" { $quotation "( chunk -- )" } } }
|
||||||
|
{ $description "A variant of " { $link with-http-get } " that checks that the response was successful." }
|
||||||
|
{ $errors "Throws an error if the HTTP request fails or is not successful." } ;
|
||||||
|
|
||||||
HELP: http-request
|
HELP: http-request
|
||||||
{ $values { "request" request } { "response" response } { "data" sequence } }
|
{ $values { "request" request } { "response" response } { "data" sequence } }
|
||||||
{ $description "Sends an HTTP request to an HTTP server, and reads the response." }
|
{ $description "Sends an HTTP request to an HTTP server, and reads the response." }
|
||||||
|
|
|
@ -176,14 +176,20 @@ ERROR: download-failed response ;
|
||||||
: http-get ( url -- response data )
|
: http-get ( url -- response data )
|
||||||
<get-request> http-request ;
|
<get-request> http-request ;
|
||||||
|
|
||||||
|
: http-get* ( url -- data )
|
||||||
|
http-get swap check-response drop ;
|
||||||
|
|
||||||
: with-http-get ( url quot: ( chunk -- ) -- response )
|
: with-http-get ( url quot: ( chunk -- ) -- response )
|
||||||
[ <get-request> ] dip with-http-request ; inline
|
[ <get-request> ] dip with-http-request ; inline
|
||||||
|
|
||||||
|
: with-http-get* ( url quot: ( chunk -- ) -- )
|
||||||
|
with-http-get check-response drop ; inline
|
||||||
|
|
||||||
: download-name ( url -- name )
|
: download-name ( url -- name )
|
||||||
present file-name "?" split1 drop "/" ?tail drop ;
|
present file-name "?" split1 drop "/" ?tail drop ;
|
||||||
|
|
||||||
: download-to ( url file -- )
|
: download-to ( url file -- )
|
||||||
binary [ [ write ] with-http-get check-response drop ] with-file-writer ;
|
binary [ [ write ] with-http-get* ] with-file-writer ;
|
||||||
|
|
||||||
: download ( url -- )
|
: download ( url -- )
|
||||||
dup download-name download-to ;
|
dup download-name download-to ;
|
||||||
|
@ -195,6 +201,9 @@ ERROR: download-failed response ;
|
||||||
: http-post ( post-data url -- response data )
|
: http-post ( post-data url -- response data )
|
||||||
<post-request> http-request ;
|
<post-request> http-request ;
|
||||||
|
|
||||||
|
: http-post* ( post-data url -- data )
|
||||||
|
http-post swap check-response drop ;
|
||||||
|
|
||||||
: <put-request> ( post-data url -- request )
|
: <put-request> ( post-data url -- request )
|
||||||
"PUT" <client-request>
|
"PUT" <client-request>
|
||||||
swap >>post-data ;
|
swap >>post-data ;
|
||||||
|
@ -202,30 +211,45 @@ ERROR: download-failed response ;
|
||||||
: http-put ( post-data url -- response data )
|
: http-put ( post-data url -- response data )
|
||||||
<put-request> http-request ;
|
<put-request> http-request ;
|
||||||
|
|
||||||
|
: http-put* ( post-data url -- data )
|
||||||
|
http-put swap check-response drop ;
|
||||||
|
|
||||||
: <delete-request> ( url -- request )
|
: <delete-request> ( url -- request )
|
||||||
"DELETE" <client-request> ;
|
"DELETE" <client-request> ;
|
||||||
|
|
||||||
: http-delete ( url -- response data )
|
: http-delete ( url -- response data )
|
||||||
<delete-request> http-request ;
|
<delete-request> http-request ;
|
||||||
|
|
||||||
|
: http-delete* ( url -- data )
|
||||||
|
http-delete swap check-response drop ;
|
||||||
|
|
||||||
: <head-request> ( url -- request )
|
: <head-request> ( url -- request )
|
||||||
"HEAD" <client-request> ;
|
"HEAD" <client-request> ;
|
||||||
|
|
||||||
: http-head ( url -- response data )
|
: http-head ( url -- response data )
|
||||||
<head-request> http-request ;
|
<head-request> http-request ;
|
||||||
|
|
||||||
|
: http-head* ( url -- data )
|
||||||
|
http-head swap check-response drop ;
|
||||||
|
|
||||||
: <options-request> ( url -- request )
|
: <options-request> ( url -- request )
|
||||||
"OPTIONS" <client-request> ;
|
"OPTIONS" <client-request> ;
|
||||||
|
|
||||||
: http-options ( url -- response data )
|
: http-options ( url -- response data )
|
||||||
<options-request> http-request ;
|
<options-request> http-request ;
|
||||||
|
|
||||||
|
: http-options* ( url -- data )
|
||||||
|
http-options swap check-response drop ;
|
||||||
|
|
||||||
: <trace-request> ( url -- request )
|
: <trace-request> ( url -- request )
|
||||||
"TRACE" <client-request> ;
|
"TRACE" <client-request> ;
|
||||||
|
|
||||||
: http-trace ( url -- response data )
|
: http-trace ( url -- response data )
|
||||||
<trace-request> http-request ;
|
<trace-request> http-request ;
|
||||||
|
|
||||||
|
: http-trace* ( url -- data )
|
||||||
|
http-trace swap check-response drop ;
|
||||||
|
|
||||||
USE: vocabs.loader
|
USE: vocabs.loader
|
||||||
|
|
||||||
{ "http.client" "debugger" } "http.client.debugger" require-when
|
{ "http.client" "debugger" } "http.client.debugger" require-when
|
||||||
|
|
Loading…
Reference in New Issue