Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
changed return type of github-api-req procedures
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eu90h committed Aug 12, 2015
1 parent 43dfc73 commit 449433e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
6 changes: 3 additions & 3 deletions gists.rkt
Expand Up @@ -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 ""])
Expand Down
15 changes: 8 additions & 7 deletions main.rkt
Expand Up @@ -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 ""])
Expand All @@ -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"))
26 changes: 19 additions & 7 deletions 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?)]
Expand All @@ -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))

Expand All @@ -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))))

Expand All @@ -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
Expand All @@ -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))))
Expand All @@ -88,4 +91,13 @@
[(jsexpr? v) (jsexpr->string v)]
[else ""]))

(define port->jsexpr (compose string->jsexpr port->string))
(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}")))))

1 comment on commit 449433e

@eu90h
Copy link
Owner Author

@eu90h eu90h commented on 449433e Aug 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also fixed raise-argument-error misuse here

Please sign in to comment.