Skip to content

Instantly share code, notes, and snippets.

@adkron
Created July 26, 2017 16:53
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 adkron/a801dbd4be99ce5ddaafacb07416326c to your computer and use it in GitHub Desktop.
Save adkron/a801dbd4be99ce5ddaafacb07416326c to your computer and use it in GitHub Desktop.
defmodule Zipato do
@moduledoc """
Entry point for inteacting with the Zipato API
"""
alias Zipato.{Authentication, Transformer, Request}
@network Application.get_env(:zipato, :network)
@credentials Application.get_env(:zipato, :credentials)
@type serial :: String.t
@type uuid :: String.t
@type error_code :: atom
@type result :: {:ok, map} | {:error, map}
@doc """
Adds the JSESSIONID to the cookies of an unauthenitcated request, thus turning it into
an authenticated request.
"""
@spec add_authentication(Request.unauthenitcated)
:: Request.authenticated_request
def add_authentication(%Request{serial: nil} = request) do
Request.add_session(request, fn ->
{:ok, session} = Authentication.get_session_id(@credentials)
session
end)
end
def add_authentication(%Request{serial: serial} = request) do
Request.add_session(request, fn ->
{:ok, session} = Authentication.get_session_id(serial, @credentials)
session
end)
end
@doc """
Preforms a request.
If the request is not authenticated it will do the authenication step and then perform the request.
"""
@spec perform(Request.t) :: result
def perform(request) do
request
|> add_authentication()
|> @network.request()
|> Transformer.transform_response()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment