summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHentioe <me@bluerain.io>2020-10-28 09:18:24 +0800
committerHentioe <me@bluerain.io>2020-10-28 09:18:24 +0800
commit00b3806ee6f8aa85910fa4e640c895a1c70f6569 (patch)
tree32a9fa7828d4f32e2bd6f9b8882854a043be006a
parentAdd helper functions (diff)
Simplify the calling process
-rw-r--r--lib/azure_ex.ex22
-rw-r--r--lib/azure_ex/dsl.ex21
-rw-r--r--lib/azure_ex/endpoint.ex20
-rw-r--r--lib/azure_ex/request.ex12
-rw-r--r--test/azure_ex/endpoint_test.exs4
-rw-r--r--test/azure_ex_test.exs4
6 files changed, 36 insertions, 47 deletions
diff --git a/lib/azure_ex.ex b/lib/azure_ex.ex
index 36dea15..7dce3c9 100644
--- a/lib/azure_ex.ex
+++ b/lib/azure_ex.ex
@@ -3,16 +3,18 @@ defmodule AzureEx do
Documentation for `AzureEx`.
"""
- @doc """
- Hello world.
+ require AzureEx.DSL
+ import AzureEx.DSL
- ## Examples
+ defendpoint(
+ "ListAllVirtualMachines",
+ "https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/virtualMachines",
+ {:get, "2020-06-01"}
+ )
- iex> AzureEx.hello()
- :world
-
- """
- def hello do
- :world
- end
+ defendpoint(
+ "GetNetworkInterface",
+ "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkInterfaces/{networkInterfaceName}",
+ {:get, "2020-06-01"}
+ )
end
diff --git a/lib/azure_ex/dsl.ex b/lib/azure_ex/dsl.ex
index 5463a9d..49647f3 100644
--- a/lib/azure_ex/dsl.ex
+++ b/lib/azure_ex/dsl.ex
@@ -3,7 +3,7 @@ defmodule AzureEx.DSL do
@path_var_re ~r/\{([^\}]+)\}/
- defmacro defendpoint(name, raw_uri) do
+ defmacro defendpoint(name, raw_uri, {method, last_api_version}) do
# 扫描原始 URI 中的路径变量
r = Regex.scan(@path_var_re, raw_uri)
# 处理并得到路径变量名称列表
@@ -21,7 +21,7 @@ defmodule AzureEx.DSL do
fun_sign_ast = {
String.to_atom(Macro.underscore(name)),
[],
- path_var_args_ast ++ [{:params, [], Elixir}]
+ path_var_args_ast ++ [{:\\, [], [{:options, [], Elixir}, []]}]
}
# 使用变量分割 URI
@@ -61,7 +61,22 @@ defmodule AzureEx.DSL do
quote do
def unquote(fun_sign_ast) do
- unquote(fun_body_ast) <> "?" <> URI.encode_query(unquote({:params, [], Elixir}))
+ api_version =
+ Keyword.get(unquote({:options, [], Elixir}), :"api-version", unquote(last_api_version))
+
+ params = Keyword.get(unquote({:options, [], Elixir}), :params)
+ data = Keyword.get(unquote({:options, [], Elixir}), :data)
+
+ endpoint = unquote(fun_body_ast) <> "?" <> "api-version=#{api_version}"
+
+ endpoint =
+ if params do
+ endpoint <> "&#{URI.encode_query(params)}"
+ else
+ endpoint
+ end
+
+ AzureEx.Request.call(endpoint, unquote(method), data)
end
end
end
diff --git a/lib/azure_ex/endpoint.ex b/lib/azure_ex/endpoint.ex
deleted file mode 100644
index 0d12cfd..0000000
--- a/lib/azure_ex/endpoint.ex
+++ /dev/null
@@ -1,20 +0,0 @@
-defmodule AzureEx.Endpoint do
- @moduledoc false
-
- require AzureEx.DSL
- import AzureEx.DSL
-
- defendpoint(
- "ListAllVirtualMachines",
- "https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/virtualMachines"
- )
-
- @doc """
- iex> AzureEx.Endpoint.get_network_interface("sub-1", "resgp-1", "net-1", api_version: "2020-1-1")
- "https://management.azure.com/subscriptions/sub-1/resourceGroups/resgp-1/providers/Microsoft.Network/networkInterfaces/net-1?api_version=2020-1-1"
- """
- defendpoint(
- "GetNetworkInterface",
- "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkInterfaces/{networkInterfaceName}"
- )
-end
diff --git a/lib/azure_ex/request.ex b/lib/azure_ex/request.ex
index 592987f..c9908f7 100644
--- a/lib/azure_ex/request.ex
+++ b/lib/azure_ex/request.ex
@@ -5,15 +5,15 @@ defmodule AzureEx.Request do
alias AzureEx.{Config, TokenHosting}
- @type params :: %{body: keyword | map}
+ @type body :: map
@type result :: any
@type error :: any
@type httpoison_result :: {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()}
- @spec call(binary, atom, params) ::
+ @spec call(binary, atom, body) ::
{:ok, result} | {:error, error}
- def call(endpoint, method, params \\ %{body: %{}}) do
- method |> send(endpoint, params) |> handle_response()
+ def call(endpoint, method, body \\ %{}) do
+ method |> send(endpoint, body) |> handle_response()
end
@spec handle_response({:ok, HTTPoison.Response.t()}) :: {:ok, result} | {:error, error}
@@ -21,8 +21,8 @@ defmodule AzureEx.Request do
{:ok, Jason.decode!(body, keys: :atoms)}
end
- @spec send(:get, String.t(), params) :: httpoison_result
- def send(:get, endpoint, %{}) do
+ @spec send(:get, String.t(), body) :: httpoison_result
+ def send(:get, endpoint, _body \\ %{}) do
headers = [Authorization: "Bearer #{TokenHosting.get_token()}"]
HTTPoison.get(endpoint, headers, Config.timeouts())
diff --git a/test/azure_ex/endpoint_test.exs b/test/azure_ex/endpoint_test.exs
deleted file mode 100644
index 719c58a..0000000
--- a/test/azure_ex/endpoint_test.exs
+++ /dev/null
@@ -1,4 +0,0 @@
-defmodule AzureEx.EndpointTest do
- use ExUnit.Case
- doctest AzureEx.Endpoint
-end
diff --git a/test/azure_ex_test.exs b/test/azure_ex_test.exs
index af9043c..d062ff5 100644
--- a/test/azure_ex_test.exs
+++ b/test/azure_ex_test.exs
@@ -1,8 +1,4 @@
defmodule AzureExTest do
use ExUnit.Case
doctest AzureEx
-
- test "greets the world" do
- assert AzureEx.hello() == :world
- end
end