diff options
author | Hentioe <me@bluerain.io> | 2020-10-22 08:33:20 +0800 |
---|---|---|
committer | Hentioe <me@bluerain.io> | 2020-10-22 08:33:20 +0800 |
commit | 22fdec619f885b4e4af7fa1935e3a38b1700211e (patch) | |
tree | 87d9a1a8327834b1d40cc3d27811605fa13dd326 | |
parent | Add dev deps (diff) |
Add basic request implementation
-rw-r--r-- | config/config.exs | 3 | ||||
-rw-r--r-- | config/dev.exs | 3 | ||||
-rw-r--r-- | config/dev.secret.exs | 6 | ||||
-rw-r--r-- | config/prod.exs | 1 | ||||
-rw-r--r-- | config/test.exs | 1 | ||||
-rw-r--r-- | lib/azure_ex/application.ex | 1 | ||||
-rw-r--r-- | lib/azure_ex/config.ex | 25 | ||||
-rw-r--r-- | lib/azure_ex/model/virtual_machine.ex (renamed from lib/model/virtual_machine.ex) | 0 | ||||
-rw-r--r-- | lib/azure_ex/model/virtual_machine_list_result.ex (renamed from lib/model/virtual_machine_list_result.ex) | 0 | ||||
-rw-r--r-- | lib/azure_ex/request.ex | 45 |
10 files changed, 85 insertions, 0 deletions
diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..8233fe9 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,3 @@ +use Mix.Config + +import_config "#{Mix.env()}.exs" diff --git a/config/dev.exs b/config/dev.exs new file mode 100644 index 0000000..8bc6858 --- /dev/null +++ b/config/dev.exs @@ -0,0 +1,3 @@ +use Mix.Config + +import_config "dev.secret.exs" diff --git a/config/dev.secret.exs b/config/dev.secret.exs new file mode 100644 index 0000000..de24332 --- /dev/null +++ b/config/dev.secret.exs @@ -0,0 +1,6 @@ +use Mix.Config + +config :azure_ex, + subscription_id: "8b0eccfd-ee43-4e98-be5d-576cb7f56a7c", + access_token: + "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImtnMkxZczJUMENUaklmajRydDZKSXluZW4zOCIsImtpZCI6ImtnMkxZczJUMENUaklmajRydDZKSXluZW4zOCJ9.eyJ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nAeoGrVA2FStfXmWTXAmV27sWod038_ncec-Vxj7k2UsgSY4UA62zbaNg7pWrQBP_nceJbnzVJCBwbA" diff --git a/config/prod.exs b/config/prod.exs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/config/prod.exs @@ -0,0 +1 @@ + diff --git a/config/test.exs b/config/test.exs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/config/test.exs @@ -0,0 +1 @@ + diff --git a/lib/azure_ex/application.ex b/lib/azure_ex/application.ex new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/lib/azure_ex/application.ex @@ -0,0 +1 @@ + diff --git a/lib/azure_ex/config.ex b/lib/azure_ex/config.ex new file mode 100644 index 0000000..bbf6238 --- /dev/null +++ b/lib/azure_ex/config.ex @@ -0,0 +1,25 @@ +defmodule AzureEx.Config do + @moduledoc false + + @default_timeout 1000 * 15 + @default_recv_timeout 1000 * 10 + + # TODO: 通过 oauth2 API 获取并自动刷新 access_token 替代硬编码配置 + @spec access_token :: String.t() | nil + def access_token, do: get(:access_token) + + @spec subscription_id :: String.t() | nil + def subscription_id, do: get(:subscription_id) + + @spec timeouts :: [timeout: integer(), recv_timeout: integer()] + def timeouts, + do: [ + timeout: get(:timeout, @default_timeout), + recv_timeout: get(:recv_timeout, @default_recv_timeout) + ] + + @spec get(atom(), any()) :: any() + defp get(key, default \\ nil) do + Application.get_env(:azure_ex, key, default) + end +end diff --git a/lib/model/virtual_machine.ex b/lib/azure_ex/model/virtual_machine.ex index 2886c14..2886c14 100644 --- a/lib/model/virtual_machine.ex +++ b/lib/azure_ex/model/virtual_machine.ex diff --git a/lib/model/virtual_machine_list_result.ex b/lib/azure_ex/model/virtual_machine_list_result.ex index 87d6475..87d6475 100644 --- a/lib/model/virtual_machine_list_result.ex +++ b/lib/azure_ex/model/virtual_machine_list_result.ex diff --git a/lib/azure_ex/request.ex b/lib/azure_ex/request.ex new file mode 100644 index 0000000..616d1d7 --- /dev/null +++ b/lib/azure_ex/request.ex @@ -0,0 +1,45 @@ +defmodule AzureEx.Request do + @moduledoc """ + HTTP request functions. + """ + + alias AzureEx.Config + + @api_version "2020-06-01" + + def endpoint(api), + do: + "https://management.azure.com/subscriptions/#{Config.subscription_id()}/providers/Microsoft.Compute/#{ + api + }?api-version=#{@api_version}" + + @type params :: %{query_params: keyword | map, body: keyword | map} + @type result :: any + @type error :: any + + @spec call(binary, atom, params) :: + {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()} + def call(api, method, params \\ %{query_params: %{}, body: %{}}) do + method |> send(endpoint(api), params) |> handle_response() + end + + @spec handle_response({:ok, HTTPosion.Response.t()}) :: {:ok, result} | {:error, error} + def handle_response({:ok, %HTTPoison.Response{body: body}}) do + body |> Jason.decode!(keys: :atoms) + end + + def send(:get, endpoint, %{query_params: query_params}) do + endpoint = endpoint <> to_query(query_params) + + HTTPoison.get(endpoint, [Authorization: "Bearer #{Config.access_token()}"], Config.timeouts()) + end + + def to_query(params, opts \\ []) when is_list(params) or is_map(params) do + prefix_question = Keyword.get(opts, :prefix_question, false) + + # TODO: 待实现 + str = "" + + if prefix_question, do: "?" <> str, else: str + end +end |