Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created Jun 20, 2020
Embed
What would you like to do?
lambda-io.demo
;; hello eql
[:app/hello]
;; data->shape
{:app/hello "ok"}
{:app/hello "ok"
:app/users [{:user/id 1
:user/username "2"
:user/addresses [{:address/description "a"
:address/number 1}]}]}
;; hello parser
[:app/hello
{:app/users [:user/id
#_:user/username
#_{:user/addresses [:address/description
:address/number]}]}]
;; get me
[:telegram.api/me]
;; get updates
[:telegram.api/updates]
;; final
[{:telegram.api/updates
[:telegram.message/text
{:telegram.message/entries [:telegram.message-entity/command
:telegram.message-entity/argument
:github.User/login
:github.User/bio]}]}]
;; final
[{:telegram.api/updates
[:telegram.message/text
{:telegram.message/entries [:telegram.message-entity/command
:telegram.message-entity/argument
:github.User/login
:github.User/bio]}]}]
;; deve estar em src/lambda_io/demo.clj
;; atenção ao underline. ele é necessário
(ns lambda-io.demo
(:require [com.wsscode.pathom.core :as p]
[com.wsscode.pathom.connect :as pc]
[com.wsscode.pathom.connect.graphql2 :as pcg]
[com.wsscode.pathom.diplomat.http.clj-http :as http.driver]
[com.wsscode.pathom.diplomat.http :as p.http]
[com.wsscode.pathom.viz.ws-connector.core :as p.connector]
[com.wsscode.pathom.trace :as pt]
[cheshire.core :as json]))
(defn qualify-map
[ns m]
(cond
(map? m) (into {}
(map (fn [[k v]]
[(keyword ns (name k))
v]))
m)
(coll? m) (map (partial qualify-map ns) m)
:else m))
(defonce indexes (atom {}))
(def telegram-bot-api-token
(System/getenv "TELEGRAM_BOT_API_TOKEN"))
(pc/defresolver hello [env input]
{::pc/output [:app/hello]}
{:app/hello "world"})
(pc/defresolver connect [env input]
{::pc/output [:app/users]}
{:app/users [{:user/id 1}
{:user/id 2}
{:user/username "first"}]})
(def connect-examples
[(pc/single-attr-resolver :user/id :user/username (fn [id]
(when (odd? id)
#_(Thread/sleep 100)
(str "username-" id))))
(pc/single-attr-resolver :user/username :user/addressess (fn [username]
[{:address/description (str "house of " username)}]))
(pc/single-attr-resolver :address/description :address/number (fn [description]
(count description)))])
(pc/defresolver telegram-get-me [_ _]
{::pc/output [:telegram.api/me]}
(let [{:keys [result]} (-> (str "https://api.telegram.org/bot" telegram-bot-api-token "/getMe")
slurp
(json/parse-string true))]
{:telegram.api/me (qualify-map "telegram.user" result)}))
(pc/defresolver telegram-update->message [_ {:telegram.update/keys [message]}]
{::pc/input #{:telegram.update/message}
::pc/output [:telegram.message/text
#_:telegram.message/message_id
:telegram.message/entries]}
(let [{:telegram.message/keys [entities text]
:as q-message} (qualify-map "telegram.message" message)]
(assoc q-message
:telegram.message/entries (for [entity entities]
(assoc (qualify-map "telegram.message-entity" entity)
:telegram.message/text text)))))
(pc/defresolver telegram-get-updates [_ _]
{::pc/output [:telegram.api/updates]}
(let [{:keys [result]} (-> (str "https://api.telegram.org/bot" telegram-bot-api-token "/getUpdates")
slurp
(json/parse-string true))]
{:telegram.api/updates (qualify-map "telegram.update" result)}))
(pc/defresolver telegram-entry-command [_ {:keys [telegram.message-entity/offset
telegram.message-entity/length
telegram.message/text]}]
{::pc/input #{:telegram.message-entity/offset
:telegram.message-entity/length
:telegram.message/text}
::pc/output [:telegram.message-entity/command]}
{:telegram.message-entity/command (keyword (subs text (inc offset)
length))
:telegram.message-entity/argument (subs text (inc (+ offset length)))})
(pc/defresolver telegram-github-command [_ {:telegram.message-entity/keys [command argument]}]
{::pc/input #{:telegram.message-entity/command
:telegram.message-entity/argument}
::pc/output [:github.User/login]}
(when (= command :github)
{:github.User/login argument}))
(comment
(parser {}
[{:telegram.api/updates
[:telegram.message/text
{:telegram.message/entries [:telegram.message-entity/command
:telegram.message-entity/argument
:github.User/login]}]}]))
(def register
[hello
connect
connect-examples
telegram-get-me
telegram-get-updates
telegram-update->message
telegram-entry-command
telegram-github-command])
(def parser
(p/parser {::p/plugins [(pc/connect-plugin {::pc/register register
::pc/indexes indexes})
pt/trace-plugin]
::p/mutate pc/mutate
::p/env {::p/reader [p/map-reader
pc/reader3
pc/open-ident-reader
p/env-placeholder-reader]
::p.http/driver http.driver/request
::p/placeholder-prefixes #{">"}}}))
(p.connector/connect-parser {::p.connector/parser-id ::parser} parser)
(defn load-github-indexes
[]
(pcg/load-index
{::pcg/url (str "https://api.github.com/graphql?access_token=" (System/getenv "GITHUB_TOKEN"))
::pcg/prefix "github"
::pcg/ident-map {"user" {"login" :github.User/login}
"repository" {"owner" :github.User/login
"name" :github.Repository/name}}
::p.http/driver http.driver/request}
indexes))
{:deps {binaryage/devtools {:mvn/version "1.0.2"}
borkdude/sci {:mvn/version "0.1.0"}
clj-http/clj-http {:mvn/version "3.10.1"}
cheshire/cheshire {:mvn/version "5.10.0"}
camel-snake-kebab {:mvn/version "0.4.1"}
com.cognitect/transit-clj {:mvn/version "1.0.324"}
com.fulcrologic/fulcro {:mvn/version "3.2.9"}
com.wsscode/pathom {:mvn/version "2.3.0-alpha9"}
com.wsscode/pathom-viz {:mvn/version "1.0.8"}
com.wsscode/pathom-viz-connector {:mvn/version "1.0.3"}
datascript/datascript {:mvn/version "0.18.13"}
hiccup/hiccup {:mvn/version "2.0.0-alpha2"}
io.pedestal/pedestal.jetty {:mvn/version "0.5.8"}
io.pedestal/pedestal.service {:mvn/version "0.5.8"}
midje/midje {:mvn/version "1.9.9"}
nrepl/bencode {:mvn/version "1.1.0"}
nubank/workspaces {:mvn/version "1.0.15"}
org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/test.check {:mvn/version "1.0.0"}
thheller/shadow-cljs {:mvn/version "2.9.10"}
ubergraph/ubergraph {:mvn/version "0.8.2"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment