simpleshop_printify/lib/simpleshop/printify/client.ex

125 lines
3.8 KiB
Elixir
Raw Normal View History

2025-11-30 11:05:49 +00:00
defmodule Simpleshop.Printify.Client do
require Logger
alias Simpleshop.Printify.TokenStore
@base_url "https://api.printify.com/v1"
@user_agent "SimpleshopMVP"
def get_products do
config = Application.get_env(:simpleshop, :printify)
shop_id = Keyword.fetch!(config, :shop_id)
url = "#{@base_url}/shops/#{shop_id}/products.json"
with_token(fn token ->
case Req.get(url, headers: headers(token)) do
{:ok, %{status: 200, body: response}} ->
products = Map.get(response, "data") || response
Logger.info("Successfully fetched #{length(products)} products")
{:ok, products}
{:ok, %{status: status, body: body}} ->
Logger.error("Failed to fetch products: #{status} - #{inspect(body)}")
{:error, "Failed to fetch products: #{status}"}
{:error, error} ->
Logger.error("HTTP error fetching products: #{inspect(error)}")
{:error, "Network error: #{inspect(error)}"}
end
end)
end
def get_product(product_id) do
config = Application.get_env(:simpleshop, :printify)
shop_id = Keyword.fetch!(config, :shop_id)
url = "#{@base_url}/shops/#{shop_id}/products/#{product_id}.json"
with_token(fn token ->
case Req.get(url, headers: headers(token)) do
{:ok, %{status: 200, body: product}} ->
Logger.info("Successfully fetched product #{product_id}")
{:ok, product}
{:ok, %{status: status, body: body}} ->
Logger.error("Failed to fetch product: #{status} - #{inspect(body)}")
{:error, "Failed to fetch product: #{status}"}
{:error, error} ->
Logger.error("HTTP error fetching product: #{inspect(error)}")
{:error, "Network error: #{inspect(error)}"}
end
end)
end
def create_order(product_id, variant_id, quantity \\ 1) do
config = Application.get_env(:simpleshop, :printify)
shop_id = Keyword.fetch!(config, :shop_id)
url = "#{@base_url}/shops/#{shop_id}/orders.json"
order_data = %{
external_id: "simpleshop-#{System.unique_integer([:positive])}",
line_items: [
%{
product_id: product_id,
variant_id: variant_id,
quantity: quantity
}
],
shipping_method: 1,
address_to: test_shipping_address()
}
with_token(fn token ->
case Req.post(url, headers: headers(token), json: order_data) do
{:ok, %{status: status, body: response}} when status in 200..299 ->
Logger.info("Successfully created order: #{inspect(response)}")
{:ok, response}
{:ok, %{status: status, body: body}} ->
Logger.error("Failed to create order: #{status} - #{inspect(body)}")
{:error, "Failed to create order: #{status} - #{inspect(body)}"}
{:error, error} ->
Logger.error("HTTP error creating order: #{inspect(error)}")
{:error, "Network error: #{inspect(error)}"}
end
end)
end
defp with_token(fun) do
case TokenStore.get_access_token() do
{:ok, token} ->
fun.(token)
{:error, :not_configured} ->
Logger.error("Printify Personal Access Token not configured in config/dev.exs")
{:error, :not_configured}
{:error, reason} ->
Logger.error("Failed to get access token: #{inspect(reason)}")
{:error, :authentication_required}
end
end
defp headers(token) do
[
{"Authorization", "Bearer #{token}"},
{"User-Agent", @user_agent},
{"Content-Type", "application/json;charset=utf-8"}
]
end
defp test_shipping_address do
%{
first_name: "John",
last_name: "Doe",
email: "test@example.com",
phone: "555-0123",
country: "US",
region: "CA",
address1: "123 Test St",
city: "San Francisco",
zip: "94102"
}
end
end