Skip to content

Commit

Permalink
change http client
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeyson committed Jan 6, 2023
1 parent 52326b8 commit b776966
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.1 (2023.01.06)

* Using `HTTPoison` over `:httpc`

## 0.1.0 (2023.01.05)

* Initial release
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ by adding `ex_azure_vision` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ex_azure_vision, "~> 0.1.0"}
{:ex_azure_vision, "~> 0.1.1"}
]
end
```
Expand All @@ -23,8 +23,7 @@ config :ex_azure_vision,
header_name: System.get_env("AZURE_OCP_APIM_HEADER_NAME"),
subscription_key: System.get_env("AZURE_OCP_APIM_SUBSCRIPTION_KEY"),
base_url: System.get_env("AZURE_COGNITIVE_VISION_BASE_URI"),
scheme: "https",
path: "/vision/v3.2"
scheme: "https"
```

## Analyze image
Expand All @@ -33,10 +32,8 @@ config :ex_azure_vision,
iex> image_url = "https://images.unsplash.com/photo-1672676831425-207e5d4a0c41?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80"

iex> query_params = %{
"visualFeatures" => "Categories,Adult,Tags,Description,Faces,Objects",
"details" => "Landmarks",
"language" => "en",
"model-version" => "latest"
visualfeatures: "Categories,Adult,Tags,Description,Faces,Objects",
details: "Landmarks"
}

iex> ExAzureVision.analyze(image_url, query_params)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_azure_vision.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defmodule ExAzureVision do
Public API functions to interact with Azure Computer Vision.
"""

defdelegate analyze(image_url, query_params), to: ExAzureVision.ImageAnalysis
defdelegate analyze(image_url, query), to: ExAzureVision.ImageAnalysis
end
47 changes: 18 additions & 29 deletions lib/ex_azure_vision/http_client.ex
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
defmodule ExAzureVision.HttpClient do
@moduledoc false
# use Tesla

def header_name, do: Application.get_env(:ex_azure_vision, :header_name)
def subscription_key, do: Application.get_env(:ex_azure_vision, :subscription_key)
def base_url, do: Application.get_env(:ex_azure_vision, :base_url)
def scheme, do: Application.get_env(:ex_azure_vision, :scheme)
def path, do: Application.get_env(:ex_azure_vision, :path)

@doc false
@spec request(String.t(), map(), String.t()) ::
{:ok, response :: map()} | {:error, reason :: atom()}
def request(image_url, query_params, service_path \\ "analyze") do
path = [path(), service_path] |> Path.join()
def request(path, image_url, query) do
url = %URI{
scheme: scheme(),
authority: base_url(),
host: base_url(),
port: 443,
path: path,
query: URI.encode_query(query)
}

url =
%URI{
host: base_url(),
scheme: scheme(),
path: path,
query: URI.encode_query(query_params)
}
|> URI.to_string()
body = Jason.encode!(%{url: image_url})
options = [ssl: [{:versions, [:"tlsv1.2"]}], recv_timeout: 30_000]

headers = [
{
String.to_charlist(header_name()),
String.to_charlist(subscription_key())
}
{"Content-Type", "application/json"},
{header_name(), subscription_key()}
]

content_type = 'application/json;'
payload = Jason.encode!(%{url: image_url})

request = {
url,
headers,
content_type,
payload
}
request = HTTPoison.post(url, body, headers, options)

case :httpc.request(:post, request, [], []) do
{:ok, {_status_code, _headers, response}} ->
{:ok, Jason.decode!(response)}
case request do
{:ok, response} ->
{:ok, Jason.decode!(response.body)}

{:error, reason} ->
{:error, reason}
Expand Down
12 changes: 5 additions & 7 deletions lib/ex_azure_vision/image_analysis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ defmodule ExAzureVision.ImageAnalysis do
query_params =
%{
"visualFeatures" => "Categories,Adult,Tags,Description,Faces,Objects",
"details" => "Landmarks",
"language" => "en",
"model-version" => "latest"
visualfeatures: "Categories,Adult,Tags,Description,Faces,Objects",
details: "Landmarks"
}
iex> annotate_image(image_url, query_params)
Expand All @@ -27,8 +25,8 @@ defmodule ExAzureVision.ImageAnalysis do
}
"""
@doc since: "0.1.0"
@spec analyze(String.t(), map()) :: {:ok, response :: map()} | {:error, reason :: atom()}
def analyze(image_url, query_params) do
HttpClient.request(image_url, query_params)
def analyze(image_url, query) do
path = "/vision/v3.2/analyze"
HttpClient.request(path, image_url, query)
end
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExAzureVision.MixProject do
use Mix.Project

@source_url "https://github.com/jaeyson/ex_azure_vision"
@version "0.1.0"
@version "0.1.1"

def project do
[
Expand Down Expand Up @@ -33,7 +33,8 @@ defmodule ExAzureVision.MixProject do
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:jason, "~> 1.4"},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.29.1", only: :dev, runtime: false}
{:ex_doc, "~> 0.29.1", only: :dev, runtime: false},
{:httpoison, "~> 1.8"}
]
end

Expand Down
9 changes: 9 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
%{
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~> 2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}

0 comments on commit b776966

Please sign in to comment.