-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrew Hebert <[email protected]>
- Loading branch information
Showing
22 changed files
with
442 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,6 @@ npm-debug.log | |
|
||
/.elixir_ls/ | ||
/log/ | ||
|
||
# local environment | ||
.env* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Recognizer.Accounts.BCCustomerUser do | ||
@moduledoc false | ||
|
||
use Ecto.Schema | ||
|
||
alias Recognizer.Accounts.User | ||
|
||
@primary_key false | ||
schema "bigcommerce_customer_users" do | ||
field :bc_id, :integer | ||
belongs_to :user, User, primary_key: true | ||
|
||
timestamps() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Recognizer.BigCommerce do | ||
@moduledoc """ | ||
BigCommerce context. | ||
""" | ||
|
||
require Logger | ||
|
||
alias Recognizer.Accounts.BCCustomerUser, as: Customer | ||
alias Recognizer.BigCommerce.Client | ||
alias Recognizer.BigCommerce.Token | ||
alias Recognizer.Repo | ||
|
||
def enabled?() do | ||
config(:enabled?) | ||
end | ||
|
||
def create_customer(user) do | ||
case Client.create_customer(user) do | ||
{:ok, bc_id} -> | ||
Repo.insert(%Customer{user_id: user.id, bc_id: bc_id}) | ||
{:ok, user} | ||
|
||
{:error, e} -> | ||
Logger.error("error creating bigcommerce customer: #{inspect(e)}") | ||
{:error, e} | ||
end | ||
end | ||
|
||
def generate_login_jwt(user) do | ||
{:ok, token, _claims} = | ||
user | ||
|> Recognizer.Repo.preload(:bigcommerce_user) | ||
|> jwt_claims() | ||
|> Token.generate_and_sign(jwt_signer()) | ||
|
||
token | ||
end | ||
|
||
def login_redirect_uri(jwt) do | ||
config(:login_uri) <> jwt | ||
end | ||
|
||
defp jwt_claims(user) do | ||
%{ | ||
"aud" => "BigCommerce", | ||
"iss" => config(:client_id), | ||
"jti" => Ecto.UUID.generate(), | ||
"operation" => "customer_login", | ||
"store_hash" => config(:store_hash), | ||
"customer_id" => user.bigcommerce_user.bc_id | ||
} | ||
end | ||
|
||
defp jwt_signer() do | ||
Joken.Signer.create("HS256", config(:client_secret)) | ||
end | ||
|
||
defp config(key) do | ||
Application.get_env(:recognizer, __MODULE__)[key] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
defmodule Recognizer.BigCommerce.Client do | ||
@moduledoc """ | ||
BigCommerce v3 | ||
""" | ||
|
||
require Logger | ||
|
||
alias Recognizer.Accounts.User | ||
|
||
alias HTTPoison.Response | ||
|
||
@default_retry_ms 5000 | ||
|
||
def create_customer(user) do | ||
with {:ok, customer_params} <- user_as_customer_params(user), | ||
{:ok, customer_json} <- Jason.encode(customer_params), | ||
{:ok, response} <- post_customer(customer_json) do | ||
get_id(response) | ||
else | ||
{:error, e} -> | ||
Logger.error("cannot create customer with error: #{inspect(e)}") | ||
{:error, e} | ||
|
||
e -> | ||
Logger.error("cannot create customer with error: #{inspect(e)}") | ||
{:error, e} | ||
end | ||
end | ||
|
||
defp user_as_customer_params(%User{email: email, first_name: first_name, last_name: last_name}) do | ||
{:ok, [%{"email" => email, "first_name" => first_name, "last_name" => last_name}]} | ||
end | ||
|
||
defp user_as_customer_params(_user) do | ||
{:error, :invalid_user} | ||
end | ||
|
||
defp post_customer(customer_json) do | ||
case http_client().post(customers_uri(), customer_json, default_headers()) do | ||
{:ok, %Response{body: response, status_code: 200}} -> | ||
{:ok, response} | ||
|
||
{:ok, %Response{status_code: 429, headers: headers}} -> | ||
sleep_for_rate_limit(headers) | ||
post_customer(customer_json) | ||
|
||
{:error, e} -> | ||
{:error, e} | ||
|
||
e -> | ||
{:error, e} | ||
end | ||
end | ||
|
||
defp get_id(response) do | ||
case Jason.decode(response) do | ||
{:ok, %{"data" => [%{"id" => id}]}} -> {:ok, id} | ||
{:error, e} -> {:error, e} | ||
e -> {:error, e} | ||
end | ||
end | ||
|
||
defp default_headers() do | ||
[ | ||
{"Content-Type", "application/json"}, | ||
{"Accept", "application/json"}, | ||
{"Authorization", "Bearer #{config(:access_token)}"}, | ||
{"x-Auth-Token", config(:access_token)} | ||
] | ||
end | ||
|
||
defp customers_uri() do | ||
uri("/v3/customers") | ||
end | ||
|
||
defp uri(path) do | ||
"https://api.bigcommerce.com/stores/#{config(:store_hash)}#{path}" | ||
end | ||
|
||
defp http_client() do | ||
config(:http_client) | ||
end | ||
|
||
defp config(key) do | ||
Application.get_env(:recognizer, Recognizer.BigCommerce)[key] | ||
end | ||
|
||
defp sleep_for_rate_limit(headers) do | ||
retry_ms = | ||
case List.keyfind(headers, "x-rate-limit-time-reset-ms", 0) do | ||
nil -> @default_retry_ms | ||
{_, retry_value} -> String.to_integer(retry_value) | ||
end | ||
|
||
Logger.warn("Rate limited, sleeping for ms: #{inspect(retry_ms)}") | ||
Process.sleep(retry_ms) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Recognizer.BigCommerce.Token do | ||
@moduledoc false | ||
|
||
use Joken.Config | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.