#lang racket ;; let's try to make a pokedex in racket! ;; first step will be to write a function to query the pokeAPI ;; like so, https://pokeapi.co/api/v2/pokemon/ditto ;; once there is a function in place that returns data I will ;; have a choice to make -- do I want to always query the API, ;; do I want to cache results, or do I want to create an offline ;; repository of poke-data to search through and sometimes update? ;; basic GET request (require net/url) (require json) ; (require nested-hash) ;; this hasn't helped me with anything yet... ;; API URL (define *POKE-API* "https://pokeapi.co/api/v2/pokemon/") (define (get-pokemon id) "queries the api, returns a butt ton of info" (call/input-url (string->url (~a *POKE-API* id)) ; ~a, like string-append but appends *either* strings or numbers to a string get-pure-port (compose string->jsexpr port->string))) ;; QUESTION: could I use read-json here, instead? (define (dex-entry id) "selects the info we need from the api, builds a hash table of the data that will build the entry" (let ([poke-data (get-pokemon id)]) (define entry-data (make-hash)) (hash-set! entry-data "name" (hash-ref poke-data 'name)) (hash-set! entry-data "id" (hash-ref poke-data 'id)) (hash-set! entry-data "sprite" (hash-ref (hash-ref poke-data 'sprites) 'front_default)) (hash-set! entry-data "stats" (hash-ref poke-data 'stats)) (hash-set! entry-data "types" (hash-ref poke-data 'types)) entry-data)) ; (dex-entry 12) ;; QUESTION: would this all be easier to handle if it were a vector or list instead of a hash table? ;; TOMORROW I'll start to figure out how to present this data (define (inspector h) "display the contents of a hash table for human eyeballs" (hash-map h (lambda (k v) (display (~a "key: " k ", value: " v "\n")))) ; QUESTION: is there a functional difference between hash-map and hash-for-each here? h) (hash-ref (inspector (dex-entry 5)) "types") ;; struggling to figure out how to access the nested hash tables stats and types... ;; the other option not yet considered is to make individual API calls for each bit of data, rather than 1 call and plucking out the data I want. ;; this may lead to an easier data shape, but means a lot more network traffic.