From 449433ec8d4b0e50b191f9bb71da2d3b05176095 Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 11 Aug 2015 20:58:18 -0400 Subject: [PATCH] changed return type of github-api-req procedures procedures with the github-api-req/c contract now return a struct called github-response, consisting of two fields: a code field and data field. the code field holds the http status code of the response, while the data field holds jsexprs --- gists.rkt | 6 +++--- main.rkt | 15 ++++++++------- utils.rkt | 26 +++++++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/gists.rkt b/gists.rkt index 6ab0310..912c6b2 100644 --- a/gists.rkt +++ b/gists.rkt @@ -67,9 +67,9 @@ (define (github-gist-starred? api-req gist-id #:media-type [media-type ""]) (= 204 - (get-status-code - (api-req (string-append "/gists/" gist-id "/star") - #:media-type media-type)))) + (github-response-code + (api-req (string-append "/gists/" gist-id "/star") + #:media-type media-type)))) (define (github-fork-gist api-req gist-id #:media-type [media-type ""]) diff --git a/main.rkt b/main.rkt index f5be8b8..412200b 100644 --- a/main.rkt +++ b/main.rkt @@ -10,7 +10,7 @@ (provide (contract-out [github-api (->* [github-identity?] [#:endpoint string? #:user-agent string?] github-api-req/c)] - [github-get-rate-limit (-> github-api-req/c jsexpr?)])) + [github-get-rate-limit (-> github-api-req/c github-response?)])) (define (github-api id #:endpoint [endpoint "api.github.com"] #:user-agent [user-agent "racket-github-api-@eu90h"]) (lambda (req #:method [method "GET"] #:data [data ""] #:media-type [media-type ""]) @@ -27,12 +27,13 @@ #:headers (if (eq? "PUT" method) (append headers (list "Content-Length: 0")) headers) #:method method #:data data)))) - - (if (or (= 201 (get-status-code (bytes->string/utf-8 status-line))) - (= 200 (get-status-code (bytes->string/utf-8 status-line))) - (= 422 (get-status-code (bytes->string/utf-8 status-line)))) - (port->jsexpr in-port) - (bytes->string/utf-8 status-line)))) + (github-response (get-status-code (bytes->string/utf-8 status-line)) + (port->jsexpr in-port)))) + ; (if (or (= 201 (get-status-code (bytes->string/utf-8 status-line))) + ; (= 200 (get-status-code (bytes->string/utf-8 status-line))) + ; (= 422 (get-status-code (bytes->string/utf-8 status-line)))) + ; (port->jsexpr in-port) + ; (bytes->string/utf-8 status-line)))) (define (github-get-rate-limit api-req) (api-req "/rate_limit")) \ No newline at end of file diff --git a/utils.rkt b/utils.rkt index 712c215..de0d261 100644 --- a/utils.rkt +++ b/utils.rkt @@ -1,11 +1,11 @@ #lang racket -(define github-api-resp/c (or/c jsexpr? string?)) -(define github-api-req/c (->* (string?) [#:method string? #:data string? #:media-type string?] github-api-resp/c)) +(define eof? ((curry equal?) eof)) (provide github-api-req/c github-api-resp/c (contract-out [struct github-identity ([type symbol?] [data (listof string?)])] + [struct github-response ([code number?] [data jsexpr?])] [get-status-code (-> string? number?)] [make-auth-header (-> symbol? (listof string?) string?)] [port->jsexpr (-> input-port? jsexpr?)] @@ -14,6 +14,9 @@ (require net/http-client openssl net/base64 json racket/list racket/string racket/port racket/contract) (struct github-identity (type data)) +(struct github-response (code data)) +(define github-api-resp/c github-response?) +(define github-api-req/c (->* (string?) [#:method string? #:data string? #:media-type string?] github-api-resp/c)) (module+ test (require rackunit)) @@ -25,9 +28,9 @@ (define (make-basic-auth-header username password) (unless (string? username) - (raise-argument-error 'make-basic-auth-header string? username)) + (raise-argument-error 'make-basic-auth-header "string?" username)) (unless (string? password) - (raise-argument-error 'make-basic-auth-header string? password)) + (raise-argument-error 'make-basic-auth-header "string?" password)) (string-append "Authorization: Basic " (base64-encode-string (string-append username ":" password)))) @@ -51,7 +54,7 @@ (string-append a-username ":" a-token))) (define (make-oauth2-header token) - (unless (string? token) (raise-argument-error 'make-oauth2-header string? token)) + (unless (string? token) (raise-argument-error 'make-oauth2-header "string?" token)) (string-append "Authorization: token " token)) (module+ test @@ -75,7 +78,7 @@ a-token)) (define (get-status-code status-line) - (unless (string? status-line) (raise-argument-error 'get-status-code string? status-line)) + (unless (string? status-line) (raise-argument-error 'get-status-code "string?" status-line)) (define parts (string-split status-line " ")) (if (< (length parts) 2) 0 (string->number (second parts)))) @@ -88,4 +91,13 @@ [(jsexpr? v) (jsexpr->string v)] [else ""])) -(define port->jsexpr (compose string->jsexpr port->string)) \ No newline at end of file +(define (port->jsexpr in) + (unless (input-port? in) + (raise-argument-error 'port->jsexpr "input-port?" in)) + (let ([data (port->string in)]) + (string->jsexpr (if (zero? (string-length data)) "{}" + data)))) +(module+ test + (check-true (jsexpr? (port->jsexpr (open-input-string "")))) + (check-true (jsexpr? (port->jsexpr + (open-input-string "{\"test\": 3}"))))) \ No newline at end of file