Created
August 13, 2019 21:52
-
-
Save nmquebb/0bf51a84885c0eecb47e5a3ec1182516 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| defmodule Providers.Gmail do | |
| @moduledoc """ | |
| API access to Gmail services. | |
| https://developers.google.com/gmail/api/guides/ | |
| """ | |
| # """ | |
| # Base API endpoint for our requests | |
| # """ | |
| @base_url "https://www.googleapis.com/gmail/v1" | |
| # """ | |
| # Gmail scopes from least to most sensitive. | |
| # https://developers.google.com/gmail/api/auth/scopes | |
| # """ | |
| @scopes [ | |
| "https://www.googleapis.com/auth/gmail.labels", | |
| "https://www.googleapis.com/auth/gmail.insert", | |
| "https://www.googleapis.com/auth/gmail.metadata", | |
| "https://www.googleapis.com/auth/gmail.settings.basic" | |
| ] | |
| def request_access() do | |
| # do auth stuff | |
| # @scopes | |
| end | |
| @doc """ | |
| Creates a filter. | |
| """ | |
| def create_filter(client, email, filter) do | |
| url = "users/" <> email <> "/settings/filters" | |
| Tesla.post(client, url, filter) | |
| end | |
| @doc """ | |
| Deletes a filter. | |
| """ | |
| def delete_filter(client, email, id) do | |
| url = "users/" <> email <> "/settings/filters" <> id | |
| Tesla.delete!(client, url) | |
| end | |
| @doc """ | |
| Gets a filter. | |
| """ | |
| def get_filter(client, email, id) do | |
| url = "users/" <> email <> "/settings/filters" <> id | |
| Tesla.get(client, url) | |
| end | |
| @doc """ | |
| Lists the message filters of a Gmail user. | |
| """ | |
| def list_filters(client, email) do | |
| url = "users/" <> email <> "/settings/filters" | |
| Tesla.get(client, url) | |
| end | |
| # build dynamic client based on runtime arguments | |
| def client(token) do | |
| middleware = [ | |
| Tesla.Middleware.JSON, | |
| {Tesla.Middleware.BaseUrl, @base_url}, | |
| {Tesla.Middleware.Headers, [{"authorization", "token: " <> token}]}, | |
| ] | |
| Tesla.client(middleware) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment