Current section
16 Versions
Jump to
Current section
16 Versions
Compare versions
22
files changed
+425
additions
-364
deletions
| @@ -3,26 +3,27 @@ | |
| 3 3 | {<<"description">>,<<"Client library for ArangoDB.">>}. |
| 4 4 | {<<"elixir">>,<<"~> 1.3">>}. |
| 5 5 | {<<"files">>, |
| 6 | - [<<"lib/xarango.ex">>,<<"lib/xarango/admin.ex">>,<<"lib/xarango/client.ex">>, |
| 7 | - <<"lib/xarango/collection.ex">>,<<"lib/xarango/connection.ex">>, |
| 8 | - <<"lib/xarango/database.ex">>,<<"lib/xarango/document.ex">>, |
| 9 | - <<"lib/xarango/edge.ex">>,<<"lib/xarango/graph.ex">>, |
| 10 | - <<"lib/xarango/index.ex">>,<<"lib/xarango/query.ex">>, |
| 6 | + [<<"lib/xarango.ex">>,<<"lib/xarango/client.ex">>, |
| 7 | + <<"lib/xarango/collection.ex">>,<<"lib/xarango/database.ex">>, |
| 8 | + <<"lib/xarango/document.ex">>,<<"lib/xarango/domain/document.ex">>, |
| 9 | + <<"lib/xarango/edge.ex">>,<<"lib/xarango/error.ex">>, |
| 10 | + <<"lib/xarango/graph.ex">>,<<"lib/xarango/index.ex">>, |
| 11 | + <<"lib/xarango/query.ex">>,<<"lib/xarango/server.ex">>, |
| 11 12 | <<"lib/xarango/simple_query.ex">>,<<"lib/xarango/task.ex">>, |
| 12 13 | <<"lib/xarango/transaction.ex">>,<<"lib/xarango/traversal.ex">>, |
| 13 | - <<"lib/xarango/user.ex">>,<<"lib/xarango/vertex.ex">>,<<"mix.exs">>, |
| 14 | - <<"README.md">>,<<"LICENSE">>]}. |
| 14 | + <<"lib/xarango/uri.ex">>,<<"lib/xarango/user.ex">>, |
| 15 | + <<"lib/xarango/vertex.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}. |
| 15 16 | {<<"licenses">>,[<<"Apache 2.0">>]}. |
| 16 17 | {<<"links">>,[{<<"GitHub">>,<<"https://github.com/beno/xarango">>}]}. |
| 17 18 | {<<"maintainers">>,[<<"Michel Benevento">>]}. |
| 18 19 | {<<"name">>,<<"xarango">>}. |
| 19 20 | {<<"requirements">>, |
| 20 | - [[{<<"app">>,<<"httpotion">>}, |
| 21 | - {<<"name">>,<<"httpotion">>}, |
| 21 | + [[{<<"app">>,<<"httpoison">>}, |
| 22 | + {<<"name">>,<<"httpoison">>}, |
| 22 23 | {<<"optional">>,false}, |
| 23 24 | {<<"requirement">>,<<"> 0.0.0">>}], |
| 24 25 | [{<<"app">>,<<"poison">>}, |
| 25 26 | {<<"name">>,<<"poison">>}, |
| 26 27 | {<<"optional">>,false}, |
| 27 28 | {<<"requirement">>,<<"> 0.0.0">>}]]}. |
| 28 | - {<<"version">>,<<"0.1.0">>}. |
| 29 | + {<<"version">>,<<"0.1.1">>}. |
| @@ -1,56 +0,0 @@ | |
| 1 | - defmodule Xarango.Admin do |
| 2 | - |
| 3 | - alias Xarango.Client |
| 4 | - |
| 5 | - #details |
| 6 | - def version(options\\[]) do |
| 7 | - url("/_api/version", options) |
| 8 | - |> Client.get |
| 9 | - |> to_config |
| 10 | - end |
| 11 | - |
| 12 | - #waitForSync, waitForCollector |
| 13 | - def flush_wal(options\\[]) do |
| 14 | - url("/_admin/wal/flush", options) |
| 15 | - |> Client.put |
| 16 | - end |
| 17 | - |
| 18 | - def set_wal_properties(wal, options\\[]) do |
| 19 | - url("/_admin/wal/properties", options) |
| 20 | - |> Client.put(wal) |
| 21 | - |> to_wal |
| 22 | - end |
| 23 | - |
| 24 | - def wal_properties(options\\[]) do |
| 25 | - url("/_admin/wal/properties", options) |
| 26 | - |> Client.get |
| 27 | - |> to_wal |
| 28 | - end |
| 29 | - |
| 30 | - |
| 31 | - defp to_config(data) do |
| 32 | - struct(Xarango.Config, data) |
| 33 | - end |
| 34 | - |
| 35 | - defp to_wal(data) do |
| 36 | - struct(Xarango.WriteAheadLog, data) |
| 37 | - end |
| 38 | - |
| 39 | - |
| 40 | - defp url(path, options) do |
| 41 | - Xarango.Connection.url(path, options) |
| 42 | - end |
| 43 | - |
| 44 | - end |
| 45 | - |
| 46 | - defmodule Xarango.Config do |
| 47 | - |
| 48 | - defstruct [:version, :server, :details] |
| 49 | - |
| 50 | - end |
| 51 | - |
| 52 | - defmodule Xarango.WriteAheadLog do |
| 53 | - |
| 54 | - defstruct [:allowOversizeEntries, :logfileSize, :historicLogfiles, :reserveLogfiles, :throttleWait, :throttleWhenPending, :syncInterval] |
| 55 | - |
| 56 | - end |
| @@ -5,12 +5,37 @@ defmodule Xarango.Client do | |
| 5 5 | def unquote(method)(url, body\\"") do |
| 6 6 | case do_request(unquote(method), url, body) do |
| 7 7 | {:ok, body} -> body |
| 8 | - {:error, error} -> raise error[:errorMessage] |
| 8 | + {:error, error} -> do_error error[:errorMessage] |
| 9 9 | end |
| 10 10 | end |
| 11 11 | end) |
| 12 | - |
| 13 | - # defp do_request(method, url, body\\"") |
| 12 | + |
| 13 | + def _url(path, options\\[]) do |
| 14 | + Xarango.Server.server.server <> path <> query_params(options) |
| 15 | + end |
| 16 | + |
| 17 | + def credentials do |
| 18 | + case Xarango.Server.server do |
| 19 | + %{username: nil} -> do_error "missing database username, set ARANGO_USER environment variable" |
| 20 | + %{password: nil} -> do_error "missing database password, set ARANGO_PASSWORD environment variable" |
| 21 | + %{username: username, password: password} -> {username, password} |
| 22 | + _ -> do_error "database credentials invalid, update `xarango.db` app config" |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def headers do |
| 27 | + {username, password} = credentials |
| 28 | + auth_header = "Basic " <> Base.encode64("#{username}:#{password}") |
| 29 | + ["Accept": "*/*", "Authorization": auth_header] |
| 30 | + #x-arango-async |
| 31 | + end |
| 32 | + |
| 33 | + defp query_params(options) do |
| 34 | + case URI.encode_query(options) do |
| 35 | + "" -> "" |
| 36 | + params -> "?" <> params |
| 37 | + end |
| 38 | + end |
| 14 39 | |
| 15 40 | defp do_request(method, url, body) when is_list(body) do |
| 16 41 | body = body |
| @@ -27,19 +52,19 @@ defmodule Xarango.Client do | |
| 27 52 | end |
| 28 53 | |
| 29 54 | defp do_request(method, url, body) when is_binary(body) do |
| 30 | - case HTTPotion.request(method, url, [body: body, headers: Xarango.Connection.headers]) do |
| 31 | - {:error, error} -> raise error |
| 32 | - response -> do_decode(response) |
| 55 | + case HTTPoison.request(method, url, body, headers) do |
| 56 | + {:error, error} -> raise Xarango.Error, message: error |
| 57 | + {:ok, response} -> do_decode(response) |
| 33 58 | end |
| 34 59 | end |
| 35 60 | |
| 36 61 | defp do_decode(response) do |
| 37 62 | case response do |
| 38 | - %HTTPotion.Response{status_code: status_code, body: body} when status_code >= 200 and status_code < 300 -> |
| 63 | + %HTTPoison.Response{status_code: status_code, body: body} when status_code >= 200 and status_code < 300 -> |
| 39 64 | {:ok, Poison.decode!(body, keys: :atoms)} |
| 40 | - %HTTPotion.Response{body: body} -> |
| 65 | + %HTTPoison.Response{body: body} -> |
| 41 66 | {:error, Poison.decode!(body, keys: :atoms)} |
| 42 | - %HTTPotion.ErrorResponse{message: message} -> raise message |
| 67 | + %HTTPoison.Error{reason: reason} -> do_error reason |
| 43 68 | end |
| 44 69 | end |
| 45 70 | |
| @@ -95,5 +120,8 @@ defmodule Xarango.Client do | |
| 95 120 | end) |
| 96 121 | end |
| 97 122 | |
| 123 | + defp do_error(msg) do |
| 124 | + raise Xarango.Error, message: msg |
| 125 | + end |
| 98 126 | |
| 99 127 | end |
| @@ -3,60 +3,69 @@ defmodule Xarango.Collection do | |
| 3 3 | :doCompact, :isVolatile, :shardKeys, :numberOfShards, :isSystem, :type, :indexBuckets, :status, :error, :count, :figures, :revision, :checksum] |
| 4 4 | |
| 5 5 | alias Xarango.Collection |
| 6 | - alias Xarango.Client |
| 6 | + import Xarango.Client |
| 7 | + use Xarango.URI, prefix: "collection" |
| 7 8 | |
| 8 9 | def collections(database\\nil) do |
| 9 10 | url("", database) |
| 10 | - |> Client.get |
| 11 | + |> get |
| 11 12 | |> Map.get(:result) |
| 12 13 | |> Enum.map(&struct(Collection, &1)) |
| 13 14 | end |
| 14 15 | |
| 15 16 | def collection(collection, database\\nil) do |
| 16 | - url("#{collection.name}", database) |
| 17 | - |> Client.get |
| 17 | + url(collection.name, database) |
| 18 | + |> get |
| 18 19 | |> parse_coll |
| 19 20 | end |
| 20 21 | |
| 21 22 | def properties(collection, database\\nil) do |
| 22 23 | url("#{collection.name}/properties", database) |
| 23 | - |> Client.get |
| 24 | + |> get |
| 24 25 | |> parse_coll |
| 25 26 | end |
| 26 27 | |
| 27 28 | def count(collection, database\\nil) do |
| 28 29 | url("#{collection.name}/count", database) |
| 29 | - |> Client.get |
| 30 | + |> get |
| 30 31 | |> parse_coll |
| 31 32 | end |
| 32 33 | |
| 33 34 | def figures(collection, database\\nil) do |
| 34 35 | url("#{collection.name}/figures", database) |
| 35 | - |> Client.get |
| 36 | + |> get |
| 36 37 | |> parse_coll |
| 37 38 | end |
| 38 39 | |
| 39 40 | def revision(collection, database\\nil) do |
| 40 41 | url("#{collection.name}/revision", database) |
| 41 | - |> Client.get |
| 42 | + |> get |
| 42 43 | |> parse_coll |
| 43 44 | end |
| 44 45 | |
| 45 46 | def checksum(collection, database\\nil) do |
| 46 47 | url("#{collection.name}/checksum", database) |
| 47 | - |> Client.get |
| 48 | + |> get |
| 48 49 | |> parse_coll |
| 49 50 | end |
| 50 51 | |
| 51 52 | def create(collection, database\\nil) do |
| 52 53 | url("", database) |
| 53 | - |> Client.post(collection) |
| 54 | + |> post(collection) |
| 54 55 | |> parse_coll |
| 55 56 | end |
| 56 57 | |
| 57 58 | def destroy(collection, database\\nil) do |
| 58 59 | url(collection.name, database) |
| 59 | - |> Client.delete |
| 60 | + |> delete |
| 61 | + end |
| 62 | + |
| 63 | + def ensure(collection, database\\nil) do |
| 64 | + try do |
| 65 | + collection(collection, database) |
| 66 | + rescue |
| 67 | + Xarango.Error -> create(collection, database) |
| 68 | + end |
| 60 69 | end |
| 61 70 | |
| 62 71 | def __destroy_all do |
| @@ -65,13 +74,13 @@ defmodule Xarango.Collection do | |
| 65 74 | |> Enum.each(&destroy(&1)) |
| 66 75 | end |
| 67 76 | |
| 68 | - defp url(path, database\\nil) do |
| 69 | - case database do |
| 70 | - nil -> "/_api/collection/#{path}" |
| 71 | - db -> "/_db/#{db.name}/_api/collection/#{path}" |
| 72 | - end |
| 73 | - |> Xarango.Connection.url |
| 74 | - end |
| 77 | + # defp url(path, database) do |
| 78 | + # case database do |
| 79 | + # nil -> "/_api/collection/#{path}" |
| 80 | + # db -> "/_db/#{db.name}/_api/collection/#{path}" |
| 81 | + # end |
| 82 | + # |> Xarango.Client._url |
| 83 | + # end |
| 75 84 | |
| 76 85 | defp parse_coll(coll) do |
| 77 86 | struct(Collection, coll) |
| @@ -1,29 +0,0 @@ | |
| 1 | - defmodule Xarango.Connection do |
| 2 | - |
| 3 | - defstruct [:server, :database, :db_version] |
| 4 | - |
| 5 | - def defaults do |
| 6 | - defaults = Application.get_env(:xarango, :db_defaults) |
| 7 | - struct(Xarango.Connection, defaults) |
| 8 | - end |
| 9 | - |
| 10 | - def url(path, options\\[]) do |
| 11 | - defaults.server <> path <> query_params(options) |
| 12 | - end |
| 13 | - |
| 14 | - def headers do |
| 15 | - [username: username, password: password] = Application.get_env(:xarango, :db_auth) |
| 16 | - auth_header = "Basic " <> Base.encode64("#{username}:#{password}") |
| 17 | - ["Accept": "*/*", "Authorization": auth_header] |
| 18 | - |
| 19 | - #x-arango-async |
| 20 | - end |
| 21 | - |
| 22 | - defp query_params(options) do |
| 23 | - case URI.encode_query(options) do |
| 24 | - "" -> "" |
| 25 | - params -> "?" <> params |
| 26 | - end |
| 27 | - end |
| 28 | - |
| 29 | - end |
| \ No newline at end of file |
Loading more files…