Skip to content

Instantly share code, notes, and snippets.

@pfeodrippe
Created February 1, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfeodrippe/e61eaaed6adcd87977fe2bb8d435e129 to your computer and use it in GitHub Desktop.
Save pfeodrippe/e61eaaed6adcd87977fe2bb8d435e129 to your computer and use it in GitHub Desktop.
{:deps
{io.github.pfeodrippe/dev-tooling {:mvn/version "1.0.48"}
cheshire/cheshire {:mvn/version "5.11.0"}}
:paths ["src"]}
^{:nextjournal.clerk/visibility {:code :hide
:result :hide}}
(ns eita
{:nextjournal.clerk/no-cache true
:nextjournal.clerk/visibility {:code :hide
:result :hide}}
(:require
[cheshire.core :as json]
[com.pfeodrippe.tooling.clerk :as tool.clerk]
[com.pfeodrippe.tooling.pathom :as tool.pathom]
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.interface.eql :as p.eql]
[nextjournal.clerk :as clerk]))
;; ◊:page-name[{:subtitle "Demo"}]{Pathom Description}
(pco/defresolver geo-from-ip [{:keys [geo-js/ip]}]
{::pco/output [:geo-js/asn
:geo-js/organization_name
:geo-js/organization
:geo-js/ip
:geo-js/timezone
:geo-js/longitude
:geo-js/latitude
:geo-js/country
:geo-js/country_code3
:geo-js/area_code
:geo-js/region
:geo-js/city
:geo-js/country_code
:geo-js/accuracy
:geo-js/continent_code]}
(-> (slurp (str "https://get.geojs.io/v1/ip/geo/" ip ".json"))
(json/parse-string #(keyword "geo-js" %))))
(pco/defresolver geo-lat-long->meta-search
[{:geo-js/keys [longitude latitude]}]
{:meta-weather/search-latt_long (str latitude "," longitude)})
(pco/defresolver meta-weather-latlong-search
[{:meta-weather/keys [search-latt_long]}]
{::pco/output [:meta-weather/distance
:meta-weather/title
:meta-weather/location_type
:meta-weather/woeid
:meta-weather/latt_long]}
(-> (slurp (str "https://www.metaweather.com/api/location/search/?lattlong=" search-latt_long))
(json/parse-string #(keyword "meta-weather" %))
first))
(pco/defresolver meta-weather-location
[{:meta-weather/keys [woeid]}]
{::pco/output
[:meta-weather/timezone_name
:meta-weather/sun_set
:meta-weather/location_type
:meta-weather/sun_rise
:meta-weather/latt_long
{:meta-weather/parent
[:meta-weather/title
:meta-weather/location_type
:meta-weather/woeid
:meta-weather/latt_long]}
:meta-weather/title
:meta-weather/woeid
{:meta-weather/sources
[:meta-weather/title
:meta-weather/slug
:meta-weather/url
:meta-weather/crawl_rate]}
:meta-weather/time
{:meta-weather/consolidated_weather
[:meta-weather/visibility
:meta-weather/weather_state_name
:meta-weather/humidity
:meta-weather/applicable_date
:meta-weather/air_pressure
:meta-weather/id
:meta-weather/weather_state_abbr
:meta-weather/predictability
:meta-weather/max_temp
:meta-weather/the_temp
:meta-weather/created
:meta-weather/wind_direction
:meta-weather/wind_speed
:meta-weather/min_temp
:meta-weather/wind_direction_compass]}
:meta-weather/timezone]}
(-> (slurp (str "https://www.metaweather.com/api/location/" woeid))
(json/parse-string #(keyword "meta-weather" %))))
(pco/defresolver meta-weather-location-the-temp
[{:meta-weather/keys [consolidated_weather]}]
{::pco/output [:meta-weather/the_temp]}
(if-let [temp (-> consolidated_weather
first
:meta-weather/the_temp)]
{:meta-weather/the_temp temp}))
(pco/defresolver its-cold? [{::keys [temperature cold-threshold]}]
{::cold? (< temperature cold-threshold)})
(def env
(pci/register
[geo-from-ip
geo-lat-long->meta-search
meta-weather-latlong-search
meta-weather-location
meta-weather-location-the-temp
its-cold?
(pbir/constantly-resolver ::cold-threshold 20)
(pbir/alias-resolver ::ip :geo-js/ip)
(pbir/alias-resolver :meta-weather/the_temp ::temperature)]))
{::clerk/visibility {:code :show :result :show}}
;; This will instrument each resolver using the output information,
;; so no real API call will be done. With clerk started, you will see
;; that you can change the query below and see instant changes in the
;; browser.
(tool.pathom/process-query
env
{::ip "198.29.213.3"}
[::temperature
::ip
::cold-threshold
:meta-weather/timezone_name])
{::clerk/visibility {:code :hide :result :hide}}
(comment
;; Start clerk.
(clerk/serve! {:watch-paths ["src"]})
;; See that the arguments are the same for a normal pathom call.
;; It just happens that the meta weather API is not working at the moment
;; of writing.
(p.eql/process
env
{::ip "198.29.213.3"}
[::temperature
::ip
::cold-threshold
:meta-weather/timezone_name])
())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment