medium v0.2.0 Medium

Medium is an Elixir library that provides an interface to interact with the Medium API.

Installation

  1. Add Medium to your list of dependencies in mix.exs:

    def deps do
      [{:medium_client, "~> 0.2.0"}]
    end
  2. Ensure Medium is started before your application:

    def application do
      [applications: [:medium_client]]
    end

Authorship and License

Medium is copyright 2016 Roberto Dip (http://roperzh.com) Medium is released under the [MIT License] (https://github.com/roperzh/medium-sdk-elixir/blob/master/LICENSE).

Summary

Functions

Generate an authorization URL

Generate a client which will use a defined token for future requests

Returns a list of contributors for a given publication

Perform a DELETE request. See request/1 or request/2 for options definition

Perform a DELETE request. See request/1 or request/2 for options definition

Perform a DELETE request. See request/1 or request/2 for options definition

Perform a GET request. See request/1 or request/2 for options definition

Perform a GET request. See request/1 or request/2 for options definition

Perform a GET request. See request/1 or request/2 for options definition

Perform a HEAD request. See request/1 or request/2 for options definition

Perform a HEAD request. See request/1 or request/2 for options definition

Perform a HEAD request. See request/1 or request/2 for options definition

Returns details of the user who has granted permission to the application

Perform a OPTIONS request. See request/1 or request/2 for options definition

Perform a OPTIONS request. See request/1 or request/2 for options definition

Perform a OPTIONS request. See request/1 or request/2 for options definition

Perform a PATCH request. See request/1 or request/2 for options definition

Perform a PATCH request. See request/1 or request/2 for options definition

Perform a PATCH request. See request/1 or request/2 for options definition

Perform a POST request. See request/1 or request/2 for options definition

Perform a POST request. See request/1 or request/2 for options definition

Perform a POST request. See request/1 or request/2 for options definition

Returns a full list of publications that the user is related to in some way

Creates a post on the authenticated user’s profile

This API allows creating a post and associating it with a publication on Medium

Perform a PUT request. See request/1 or request/2 for options definition

Perform a PUT request. See request/1 or request/2 for options definition

Perform a PUT request. See request/1 or request/2 for options definition

Perform a request. See request/2 for available options

Perform a request using client function

Perform a TRACE request. See request/1 or request/2 for options definition

Perform a TRACE request. See request/1 or request/2 for options definition

Perform a TRACE request. See request/1 or request/2 for options definition

Types

option()
option ::
  {:method, Tesla.Env.method} |
  {:url, Tesla.Env.url} |
  {:query, Tesla.Env.query} |
  {:headers, Tesla.Env.headers} |
  {:body, Tesla.Env.body} |
  {:opts, Tesla.Env.opts}

Functions

authorize_url(query)

Generate an authorization URL

Parameters for the url must be provided as a map, with the following required keys:

  • client_id, String
  • scope, List of String elements
  • state, String
  • response_type, String
  • redirect_uri, String

For more details, please check the official [documentation] (https://github.com/Medium/medium-api-docs#21-browser-based-authentication)

client(token, url \\ "https://api.medium.com/v1")

Generate a client which will use a defined token for future requests

Optionally the client accepts a custom base url to be used for all request this can be useful for future api versions and testing.

Examples

client = Medium.client("my-access-token")
test_client = Medium.client("my-acces-token", "http://localhost")
contributors(client, publication_id)

Returns a list of contributors for a given publication.

Please check the official [documentation] (https://github.com/Medium/medium-api-docs#listing-the-users-publications)

Examples

contributors =
  token
  |> Medium.client
  |> Medium.publications("publication_id")
delete(url)

Perform a DELETE request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.delete("/users")
delete(client, url)

Perform a DELETE request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.delete("/users")
iex> ExampleApi.delete("/users", query: [page: 1])
delete(client, url, options)

Perform a DELETE request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.delete("/users", query: [page: 1])
get(url)

Perform a GET request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.get("/users")

Perform a GET request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.get("/users")
iex> ExampleApi.get("/users", query: [page: 1])
get(client, url, options)

Perform a GET request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.get("/users", query: [page: 1])
get_token(query, url \\ "https://api.medium.com/v1")

Request for a token

Parameters for the url must be provided as a map, with the following required keys:

  • code, String generated with the url provided via authorize_url/1
  • client_id, String
  • client_secret, String
  • grant_type, String
  • redirect_uri, String

For more details, please check the official [documentation] (https://github.com/Medium/medium-api-docs#21-browser-based-authentication)

head(url)

Perform a HEAD request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.head("/users")

Perform a HEAD request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.head("/users")
iex> ExampleApi.head("/users", query: [page: 1])
head(client, url, options)

Perform a HEAD request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.head("/users", query: [page: 1])
me(client)

Returns details of the user who has granted permission to the application.

Please check the official documentation

Examples

user_info = Medium.client("token") |> Medium.me
user_info.username //=> "roperzh"
user_info.image_url //=> "https://images.medium.com/0*fkfQT7TlUGGyI.png"
options(url)

Perform a OPTIONS request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.options("/users")
options(client, url)

Perform a OPTIONS request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.options("/users")
iex> ExampleApi.options("/users", query: [page: 1])
options(client, url, options)

Perform a OPTIONS request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.options("/users", query: [page: 1])
patch(url, body)

Perform a PATCH request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.patch("/users", %{name: "Jon"})

Perform a PATCH request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.patch("/users", %{name: "Jon"})
iex> ExampleApi.patch("/users", %{name: "Jon"}, query: [scope: "admin"])
patch(client, url, body, options)

Perform a PATCH request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.patch("/users", %{name: "Jon"}, query: [scope: "admin"])
post(url, body)

Perform a POST request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.post("/users", %{name: "Jon"})

Perform a POST request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.post("/users", %{name: "Jon"})
iex> ExampleApi.post("/users", %{name: "Jon"}, query: [scope: "admin"])
post(client, url, body, options)

Perform a POST request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.post("/users", %{name: "Jon"}, query: [scope: "admin"])
publications(client, user_id)

Returns a full list of publications that the user is related to in some way.

Please check the official [documentation] (https://github.com/Medium/medium-api-docs#listing-the-users-publications)

Examples

publications = Medium.client("token") |> Medium.publications("user_id")
publish(client, author_id, publication)

Creates a post on the authenticated user’s profile.

Please check the official documentation

Examples

resp = Medium.client("token") |> Medium.publish("user_id", publication)
publish_comment(client, publication_id, publication)

This API allows creating a post and associating it with a publication on Medium.

Please check the official [documentation] (http://github.com/Medium/medium-api-docs#creating-a-post-under-a-publication)

Examples

resp =
  token
  |> Medium.client
  |> Medium.publish_comment("publication_id", publication_data)
put(url, body)

Perform a PUT request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.put("/users", %{name: "Jon"})

Perform a PUT request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.put("/users", %{name: "Jon"})
iex> ExampleApi.put("/users", %{name: "Jon"}, query: [scope: "admin"])
put(client, url, body, options)

Perform a PUT request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.put("/users", %{name: "Jon"}, query: [scope: "admin"])
request(options)
request([option]) :: Tesla.Env.t

Perform a request. See request/2 for available options.

request(client, options)

Perform a request using client function

Options:

  • :method - the request method, one of [:head, :get, :delete, :trace, :options, :post, :put, :patch]
  • :url - either full url e.g. “http://example.com/some/path” or just “/some/path” if using Tesla.Middleware.BaseUrl
  • :query - a keyword list of query params, e.g. [page: 1, per_page: 100]
  • :headers - a keyworld list of headers, e.g. [{"content-type", "text/plain"}]
  • :body - depends on used middleware:

    • by default it can be a binary
    • if using e.g. JSON encoding middleware it can be a nested map
    • if adapter supports it it can be a Stream with any of the above
  • :opts - custom, per-request middleware or adapter options

Examples:

iex> ExampleApi.request(method: :get, url: "/users/path")

You can also use shortcut methods like:

iex> ExampleApi.get("/users/1")

or

iex> myclient |> ExampleApi.post("/users", %{name: "Jon"})
trace(url)

Perform a TRACE request. See request/1 or request/2 for options definition.

Example

iex> ExampleApi.trace("/users")
trace(client, url)

Perform a TRACE request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.trace("/users")
iex> ExampleApi.trace("/users", query: [page: 1])
trace(client, url, options)

Perform a TRACE request. See request/1 or request/2 for options definition.

Example

iex> myclient |> ExampleApi.trace("/users", query: [page: 1])