summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHentioe <me@bluerain.io>2020-10-22 08:33:20 +0800
committerHentioe <me@bluerain.io>2020-10-22 08:33:20 +0800
commit22fdec619f885b4e4af7fa1935e3a38b1700211e (patch)
tree87d9a1a8327834b1d40cc3d27811605fa13dd326 /lib
parentAdd dev deps (diff)
Add basic request implementation
Diffstat (limited to 'lib')
-rw-r--r--lib/azure_ex/application.ex1
-rw-r--r--lib/azure_ex/config.ex25
-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.ex45
5 files changed, 71 insertions, 0 deletions
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