-
-
Save louis77/47f836e3bae2a817889d81c83e2ff1bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require (only-in net/http-easy | |
get | |
response-status-code | |
response-json) | |
web-server/servlet-env | |
web-server/http/json | |
web-server/http/request-structs | |
web-server/http/empty | |
json) | |
;; Trustyou API | |
(define trustyou-scale5-url-template | |
"http://api.trustyou.com/hotels/~a/seal.json?scale=5") | |
(define (fetch-trustyou hotel-id) | |
(define resp (get (format trustyou-scale5-url-template hotel-id))) | |
(if (equal? (response-status-code resp) 200) | |
(hash-ref (response-json resp) 'response #f) | |
#f)) | |
;; Hook Request looks like | |
;; event, id, resource, data (id, ranks, externals, references, tags) | |
(define (trustyou-tag tag) | |
((listof string?) . -> . (or/c #f string?)) | |
(let ([tag (string-split tag ":" #:trim? #t)]) | |
(and | |
(> (length tag) 1) | |
(equal? (first tag) "trustyou") | |
(second tag)))) | |
(define (trustyou-rank rank) | |
(define provider (hash-ref rank 'provider #f)) | |
(define external_id (hash-ref rank 'external_id #f)) | |
(and | |
(equal? provider "trustyou") | |
external_id)) | |
(define (extract-trustyou-id req) | |
(define data (hash-ref req 'data)) | |
(or | |
(ormap trustyou-tag (hash-ref data 'tags)) | |
(ormap trustyou-rank (hash-ref data 'ranks)) | |
(ormap (lambda (a) | |
(trustyou-rank (hash-ref a 'ranks))) (hash-ref data 'references)))) | |
(define not-found-response | |
(response/empty #:code 204)) | |
(define (parse-request req) | |
(bytes->jsexpr (request-post-data/raw req))) | |
(define (start req) | |
(define req-json (parse-request req)) | |
(define trustyou-id (extract-trustyou-id req-json)) | |
(if trustyou-id | |
(let ([trustyou-data (fetch-trustyou trustyou-id)]) | |
(if trustyou-data | |
(response/jsexpr (hasheq 'data | |
trustyou-data | |
'request req-json)) | |
not-found-response)) | |
not-found-response)) | |
(serve/servlet start | |
#:stateless? #t | |
#:command-line? #t | |
#:servlet-regexp #rx"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment