defmodule ExStreamClient.Operations.CheckSns do @moduledoc ~S""" Modules for interacting with the `check_sns` group of Stream APIs API Reference: https://getstream.github.io/protocol/?urls.primaryName=Chat%20v2 ### Shared options All functions in this module accept the following optional parameters: * `api_key` - API key to use. If not provided, the default key from config will be used * `api_key_secret` - API key secret to use. If not provided, the default secret from config will be used * `endpoint` - endpoint to use. If not provided, the default endpoint from config will be used * `client` - HTTP client to use. Must implement `ExStreamClient.Http.Behavior`. Defaults to `ExStreamClient.Http` * `req_opts` - all of these options will be forwarded to req. See `Req.new/1` for available options """ require Logger @doc ~S""" Validates Amazon SNS configuration ### Required Arguments: - `payload`: `Elixir.ExStreamClient.Model.CheckSNSRequest` All options from [Shared Options](#module-shared-options) are supported. """ @spec check_sns(ExStreamClient.Model.CheckSNSRequest.t()) :: {:ok, ExStreamClient.Model.CheckSNSResponse.t()} | {:error, any()} def check_sns(payload, opts \\ []) do client = get_client(opts) request_opts = [url: "/api/v2/check_sns", method: :post, params: []] ++ [json: payload] request_opts = Keyword.merge(request_opts, Keyword.get(opts, :req_opts, [])) r = Req.new(request_opts) |> Req.Request.append_response_steps( decode: fn {request, response} -> response_type = if response.status in 200..299 do :ok else :error end response_handlers = %{ 201 => ExStreamClient.Model.CheckSNSResponse, 400 => ExStreamClient.Model.APIError, 429 => ExStreamClient.Model.APIError } parsed = case Map.get(response_handlers, response.status) do nil -> {:error, response.body} mod -> {response_type, mod.decode(response.body)} end {request, %{response | body: parsed}} end ) case client.request(r, get_request_opts(opts)) do {:ok, response} -> response.body {:error, error} -> {:error, error} end end defp get_client(opts) do client = Keyword.get(opts, :client, ExStreamClient.Http) unless Code.ensure_loaded?(client) and function_exported?(client, :request, 2) do raise ArgumentError, "client #{inspect(client)} must implement request/2 to conform to ExStreamClient.Http.Behavior" end client end defp get_request_opts(opts) do Keyword.take(opts, [:api_key, :api_key_secret, :endpoint]) end end