Current section
Files
Jump to
Current section
Files
lib/tanuki.ex
defmodule Tanuki do
use HTTPoison.Base
alias Tanuki.Client
@moduledoc """
## Tanuki
At this moment this wrapper is a work in progress!
To use this library you'll need your private token for your gitlab instance, or from
gitlab. This can be found at <Gitlab Instance URL>profile/account
Some functions are only to be used by admins.
Let's get started! :)
iex> Tanuki.Client.new("T0k3n_H3r3", "https://yourinstance.tld/api/v3/")
%Tanuki.Client{endpoint: "https://yourinstance.tld/api/v3/", private_token: "T0k3n_H3r3"}
iex> Tanuki.Client.new("T0k3n_H3r3") # Default is GitLab.com
%Tanuki.Client{endpoint: "https://gitlab.com/api/v3/", private_token: "T0k3n_H3r3"}
With the returned client you can make requests to the endpoint specified.
"""
@doc """
Used for all GET requests
Shouldn't be called by you.
"""
def get(path, client, body \\ ""), do: url(client, path) |> json_request(:get, body, client)
@doc """
Used for all POST requests
Shouldn't be called by you.
"""
def post(path, client, body \\ ""), do: url(client, path) |> json_request(:post, body, client)
@doc """
Used for all PUT requests
Shouldn't be called by you.
"""
def put(path, client, body \\ []), do: url(client, path) |> json_request(:put, body, client)
@doc """
Used for all PATCH requests
Shouldn't be called by you.
"""
def patch(path, client, body \\ []), do: url(client, path) |> json_request(:patch, body, client)
@doc """
Used for all DELETE requests
Shouldn't be called by you.
"""
def delete(path, client, body \\ []), do: url(client, path) |> json_request(:delete, body, client)
defp json_request(url, method, body, client) do
body = Poison.encode!(body)
header = prepare_headers(client)
request!(method, url, body, header)
end
def process_response_body(body), do: body |> Poison.decode!
def prepare_headers(%Client{private_token: token}) do
[{"Accept", "application/json"}, {"PRIVATE-TOKEN", token}, {"CONTENT-TYPE", "application/json"}]
end
def process_response(%HTTPoison.Response{status_code: 200, body: ""}), do: ""
def process_response(%HTTPoison.Response{status_code: 200, body: body}), do: Poison.decode!(body)
def process_response(%HTTPoison.Response{status_code: status_code, body: ""}), do: { status_code, nil }
def process_response(%HTTPoison.Response{status_code: status_code, body: body }), do: { status_code, Poison.decode!(body) }
defp url(%Client{endpoint: endpoint}, path), do: endpoint <> path
end