Commits

Anonymous committed 4ea2957

Ensure we depend on all the eggs that our generated code requires, except http-client

We don't specify the egg that provides call-with-input-request. This causes
the variable to remain free and therefore it binds to whatever the user has
in their environment. This allows them to choose between the
call-with-input-request in the oauth and http-client eggs. This patch
includes a few examples of how this works.

Signed-off-by: Andy Bennett <andyjpb@knodium.com>

  • Participants
  • Parent commits f06d065

Comments (0)

Files changed (3)

  ; Notice that you should NOT put Chicken units (e.g., srfi-1, srfi-13
  ; and many others) in `needs' or in `depends'.
  ;(needs nothing)
+ (needs intarweb uri-common)
 
  ; A list of eggs required for TESTING ONLY.  See the `Tests' section.
  ; Just like `needs' and `depends', `test-depends' should NOT contain
 (import chicken scheme)
 (import-for-syntax chicken srfi-1)
 (use data-structures)
+(use intarweb uri-common)
 
 ; (define-method (name args...) endpoint writer reader #!optional header)
 ; -> if no writer is provided, generates a procedure (name #!optional args...)

rest-bind.svnwiki

 
 When the {{procedure-name}} is called, the arguments are put into the URL. If {{body-writer}} is specified then it it is called with the value of the last postitional argument ({{path-arg}}). {{body-writer}} should return something suitable for use as the {{writer}} argument of {{call-with-input-request}}.
 
-When all the preparations have been made, the URL is then fetched via {{call-with-input-request}} from [[http-client]]. If {{body-writer}} is not specified then the GET method is implied. If {{body-writer}} is specified then the POST method is implied. If the HTTP API calls for another method then {{uri-or-request}} must be specified using {{make-request}} from [[intarweb]]. {{body-reader}} is passed as the {{reader}} argument to {{call-with-input-request}}. The result of {{body-reader}} is one of the results of the call to {{procedure-name}}.
+When all the preparations have been made, the URL is then fetched via
+{{call-with-input-request}}. {{call-with-input-request}} remains free inside
+define-method so will bind to whichever procedure is bound in the environment
+from which {{define-method}} is called. This means that, by (use ...)ing the
+correct module, you can use the regular {{call-with-input-request}} from
+[[http-client]] or you can use the version from [[oauth]] which signs the
+requests in the appropriate manner. If {{body-writer}} is not specified then
+the GET method is implied. If {{body-writer}} is specified then the POST method
+is implied. If the HTTP API calls for another method then {{uri-or-request}}
+must be specified using {{make-request}} from [[intarweb]]. {{body-reader}} is
+passed as the {{reader}} argument to {{call-with-input-request}}. The result of
+{{body-reader}} is one of the results of the call to {{procedure-name}}.
 
 When {{header-reader}} is specified, {{procedure-name}} returns three results:
 the result of the call to {{header-reader}}, the result of the call to
 
 === Examples
 
+==== 1
 See [[https://bitbucket.org/knodium/dropbox/src/e7929501b4fe2fee4c5e821cfc925b22bb51825d/dropbox-lolevel.scm|dropbox-lolevel.scm]] file for more examples.
 
 Here we bind to
 read-json from [[medea]]. The request uses the GET method and has no
 body so we substitute #f for the writer procedure.
 
+The Dropbox API requires the requests to be signed with oauth so use
+(use oauth-client) which provides a call-with-input-request that will sign the
+requests for us.
+
+    (use rest-bind oauth-client)
+    
     (define-method (metadata root path #!key file_limit hash list include_deleted rev locale)
     "https://api.dropbox.com/1/metadata/"
     #f
     read-json)
 
+==== 2
+
+Here we bind to
+    http://www.call-cc.org
+...which just causes the generated procedure to return the contents of the
+Chicken Homepage.
+
+This "API" does not require the requests to be signed with oauth, so we want
+the method to use the regular call-with-input request from http-client.
+
+    (use rest-bind http-client)
+    
+    (define-method (homepage)
+    "https://www.call-cc.org/"
+    #f
+    (cut read-string #f <>))
+
 
 === License