]> gitweb.factorcode.org Git - factor.git/commitdiff
google.gmail: GMail API support
authorBjörn Lindqvist <bjourne@gmail.com>
Mon, 17 Oct 2016 11:09:43 +0000 (13:09 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Mon, 17 Oct 2016 11:09:43 +0000 (13:09 +0200)
extra/google/gmail/authors.txt [new file with mode: 0644]
extra/google/gmail/gmail.factor [new file with mode: 0644]

diff --git a/extra/google/gmail/authors.txt b/extra/google/gmail/authors.txt
new file mode 100644 (file)
index 0000000..8c0a152
--- /dev/null
@@ -0,0 +1 @@
+Björn Lindqvist
diff --git a/extra/google/gmail/gmail.factor b/extra/google/gmail/gmail.factor
new file mode 100644 (file)
index 0000000..b518c22
--- /dev/null
@@ -0,0 +1,40 @@
+! Copyright (C) 2016 Björn Lindqvist.
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays json.reader kernel namespaces oauth2 sequences ;
+IN: google.gmail
+
+CONSTANT: api-base "https://www.googleapis.com/gmail/v1/users"
+
+CONSTANT: auth-uri "https://accounts.google.com/o/oauth2/auth"
+CONSTANT: token-uri "https://www.googleapis.com/oauth2/v4/token"
+CONSTANT: redirect-uri "urn:ietf:wg:oauth:2.0:oob"
+CONSTANT: gmail-scope-ro "https://www.googleapis.com/auth/gmail.readonly"
+
+SYMBOL: access-token
+
+: configure-oauth2 ( client-id client-secret -- )
+    [ auth-uri token-uri redirect-uri ] 2dip gmail-scope-ro { }
+    oauth2 boa \ oauth2 set ;
+
+: ensure-token ( -- )
+    access-token [
+        [ \ oauth2 get console-flow ] unless*
+    ] change ;
+
+: api-call ( method get-params -- result )
+    ensure-token
+    [ api-base prepend ] dip string+params>url
+    access-token get oauth-http-get nip
+    json> ;
+
+: list-drafts ( -- seq )
+    "/me/drafts" { } api-call ;
+
+: list-labels ( -- seq )
+    "/me/labels" { } api-call ;
+
+: list-messages-by-label ( label -- seq )
+    [ "/me/messages" ] dip "labelIds" swap 2array 1array api-call ;
+
+: get-messages ( id format -- seq )
+    [ "/me/messages/" prepend ] dip "format" swap 2array 1array api-call ;