Skip to content

Instantly share code, notes, and snippets.

@mndvns
Last active January 22, 2016 23:12
Show Gist options
  • Select an option

  • Save mndvns/edc4f11196e29e299520 to your computer and use it in GitHub Desktop.

Select an option

Save mndvns/edc4f11196e29e299520 to your computer and use it in GitHub Desktop.
Poe API
15:17:13.350 [error] Ranch listener Api.HTTP.HTTP had connection process started with :cowboy_protocol:start_link/4 at #PID<0.448.0> exit with reason: {{%ArgumentError{message: "argument error"}, [{:erlang, :byte_size, [nil], []}, {Api.Service.Snow, :do_get, 4, [file: 'lib/api/service/snow.ex', line: 46]}, {Api.Resource.Cases.Hyperjson, :etude_14667509_exec, 5, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 7]}, {Api.Resource.Cases.Hyperjson, :etude_14667509, 4, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 7]}, {Api.Resource.Cases.Hyperjson, :etude_var_22987114, 4, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 6]}, {Api.Resource.Cases.Hyperjson, :etude_104760913, 4, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 14]}, {Api.Resource.Cases.Hyperjson, :etude_27645449, 4, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 1]}, {Api.Resource.Cases.Hyperjson, :etude_64670687, 4, [file: '/Users/michaelvanasse/Projects/optiv/poe-service-now-proxy/lib/api/resource/cases.ex', line: 1]}]}, {Api.HTTP, :call, [%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.448.0>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["favicon.ico"], peer: {{127, 0, 0, 1}, 61534}, port: 4000, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"}, {"accept", "*/*"}, {"referer", "http://localhost:4000/changes"}, {"accept-encoding", "gzip, deflate, sdch"}, {"accept-language", "en-US,en;q=0.8,pt;q=0.6"}, {"cookie", "_ga=GA1.1.1028465968.1453332872"}], request_path: "/favicon.ico", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: nil, state: :unset, status: nil}, []]}}
defmodule Api.HTTP do
use PoeApi.HTTP
get "/", Api.Resource.Root
get "/users/:user", Api.Resource.Users.Read
get "/:type", Api.Resource.Cases
get "/:type/create", Api.Resource.Cases.Create
get "/:type/:item", Api.Resource.Cases.Read
post "/:type/:item", Api.Resource.Cases.Update
delete "/:type/:item", Api.Resource.Cases.Remove
end
defmodule Api.Resource.Cases do
use PoeApi.Resource
param type
let cases = Case.get_all(type)
hyper do
action do
%{
"count" => length(cases),
"collection" => for item <- cases do
link_to(Api.Resource.Cases.Read, type: type, item: item["sys_id"])
|> ^Map.merge(item)
end,
"create" => link_to(Api.Resource.Cases.Create, type: type)
}
end
end
end
defmodule Api.Resource.Cases.Read do
use PoeApi.Resource
param type
param item do
Case.get(type, value)
end
hyper do
action do
%{
"update" => link_to(Api.Resource.Cases.Update, type: type, item: item["sys_id"]),
"remove" => link_to(Api.Resource.Cases.Remove, type: type, item: item["sys_id"])
} |> ^Map.merge(item)
end
end
end
defmodule Api.Service.Snow do
require Logger
@username :simple_env.get_binary("SNOW_USERNAME")
@password :simple_env.get_binary("SNOW_PASSWORD")
@company :simple_env.get_binary("SNOW_COMPANY")
@auth "Basic " <> Base.encode64(@username <> ":" <> @password)
@headers %{"accepts" => "application/json", "authorization" => @auth}
@host "https://fishnetsecuritydev.service-now.com"
@path @host <> "/api/now/table/"
def get(path) do
do_get(path, %{}, [], [])
end
def get(path, fill) do
do_get(path, %{}, fill, [])
end
def get(path, fill, fields) do
do_get(path, %{}, fill, fields)
end
def get_one(path, id) do
do_get(path <> "/" <> id, %{}, [], [])
end
def get_one(path, id, fields) do
do_get(path <> "/" <> id, %{}, [], fields)
end
def update(_path, _id, body) do
IO.inspect(body)
end
defp do_get(path, query \\ %{}, fill \\ [], fields \\ []) do
qs = "?sysparm_fields=" <> (Enum.concat(["sys_id"], fields) |> Enum.join(","))
if Enum.member?(fill, "company") do
qs = qs <> "sysparm_query=company=" <> @company <> "^ORDERBYDESCopened_at"
end
IO.inspect(qs)
uri = @path <> path <> qs
IO.puts("!!!! " <> uri)
case HTTPoison.get(uri, @headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, body} = Poison.decode(body)
{:ok, body["result"]}
# {:error, %HTTPoison.Error{reason: reason}} ->
# IO.inspect reason
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment