Created
July 26, 2017 16:53
-
-
Save adkron/a801dbd4be99ce5ddaafacb07416326c 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 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