diff --git a/config/config.exs b/config/config.exs index c0d2fa7..0750a1f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -29,7 +29,8 @@ config :tai_shang_micro_faas_system, did_mainnet: "0x61b96051f553d767d7e6dfcc04b04c28d793c8af3d07d3a43b4e2f8f4ca04c9f", did_testnet: "0xc71124a51e0d63cfc6eb04e690c39a4ea36774ed4df77c00f7cbcbc9d0505b2c", api_key: System.get_env("API_KEY"), - admin_key: System.get_env("ADMIN_KEY") + admin_key: System.get_env("ADMIN_KEY"), + clipdrop_key: System.get_env("CLIPDROP_KEY") config :cors_plug, max_age: 2592000, diff --git a/lib/components/ai_pic.ex b/lib/components/ai_pic.ex new file mode 100644 index 0000000..d2e69d9 --- /dev/null +++ b/lib/components/ai_pic.ex @@ -0,0 +1,16 @@ +defmodule Components.AiPic do + + @api %{text_to_image: "https://clipdrop-api.co/text-to-image/v1"} + alias Components.ExHttp + def text_to_image(txt) do + end + + def do_text_to_image(txt) do + IO.puts inspect Constants.clipdrop_key() + body = "prompt=#{txt}" + heads = + [{"Content-Type", "text/plain"}, {"x-api-key", Constants.clipdrop_key()}] + IO.puts inspect heads + ExHttp.http_post(@api.text_to_image, body, heads, 3) + end +end \ No newline at end of file diff --git a/lib/components/ex_http.ex b/lib/components/ex_http.ex index c8866ad..80feff8 100644 --- a/lib/components/ex_http.ex +++ b/lib/components/ex_http.ex @@ -38,6 +38,32 @@ defmodule Components.ExHttp do {:error, "POST retires #{@retries} times and not success"} end + def http_post(_url, _data, _, retries) when retries == 0 do + {:error, "POST retires #{@retries} times and not success"} + end + + def http_post(url, data, heads, retries) do + body = Poison.encode!(data) + + url + |> HTTPoison.post( + body, + # [{"User-Agent", @default_user_agent}, {"Content-Type", "text/plain"}] + heads, + hackney: [headers: [{"User-Agent", @default_user_agent}]] + ) + |> handle_response() + |> case do + {:ok, body} -> + {:ok, body} + {:error, 404} -> + {:error, 404} + {:error, _} -> + Process.sleep(500) + http_post(url, data, heads, retries - 1) + end + end + def http_post(url, data, retries) do body = Poison.encode!(data) diff --git a/lib/utils/constants.ex b/lib/utils/constants.ex index b72784c..8d5597a 100644 --- a/lib/utils/constants.ex +++ b/lib/utils/constants.ex @@ -60,4 +60,9 @@ defmodule Constants do def get_admin_key() do Application.fetch_env!(:tai_shang_micro_faas_system, :admin_key) end + + def clipdrop_key() do + :tai_shang_micro_faas_system + |> Application.fetch_env!(:clipdrop_key) + end end