Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #508 from Shopify/partnersapi-from-helpers-to-shop…
Browse files Browse the repository at this point in the history
…ifycli

Moving ShopifyCli::Helpers::PartnersAPI to ShopifyCli::PartnersAPI
  • Loading branch information
mkevinosullivan authored Apr 14, 2020
2 parents 1f1b0e6 + 0c44592 commit 1a7c267
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 177 deletions.
6 changes: 3 additions & 3 deletions lib/shopify-cli/commands/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fetch_org
org_id = if orgs.count == 1
orgs.first["id"]
else
CLI::UI::Prompt.ask('Which organization does this project belong to?') do |handler|
CLI::UI::Prompt.ask('To which organization does this project belong?') do |handler|
orgs.each { |org| handler.option(org["businessName"]) { org["id"] } }
end
end
Expand All @@ -33,7 +33,7 @@ def get_app(apps)
app_id = if apps.count == 1
apps.first["id"]
else
CLI::UI::Prompt.ask('Which app does this project belong to?') do |handler|
CLI::UI::Prompt.ask('To which app does this project belong?') do |handler|
apps.each { |app| handler.option(app["title"]) { app["id"] } }
end
end
Expand All @@ -44,7 +44,7 @@ def get_shop(shops, id)
if shops.count == 1
shops.first
elsif shops.count == 0
@ctx.puts('No developement shops available.')
@ctx.puts('No development stores available.')
@ctx.puts("Visit {{underline:https://partners.shopify.com/#{id}/stores}} to create one")
else
shop = CLI::UI::Prompt.ask('Which development store would you like to use?') do |handler|
Expand Down
1 change: 0 additions & 1 deletion lib/shopify-cli/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Helpers
autoload :Haikunator, 'shopify-cli/helpers/haikunator'
autoload :Node, 'shopify-cli/helpers/node'
autoload :Organizations, 'shopify-cli/helpers/organizations'
autoload :PartnersAPI, 'shopify-cli/helpers/partners_api'
autoload :PkceToken, 'shopify-cli/helpers/pkce_token'
autoload :Store, 'shopify-cli/helpers/store'
autoload :String, 'shopify-cli/helpers/string'
Expand Down
6 changes: 3 additions & 3 deletions lib/shopify-cli/helpers/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ module Helpers
class Organizations
class << self
def fetch_all(ctx)
resp = Helpers::PartnersAPI.query(ctx, 'all_organizations')
resp = ShopifyCli::PartnersAPI.query(ctx, 'all_organizations')
resp['data']['organizations']['nodes'].map do |org|
org['stores'] = org['stores']['nodes']
org
end
end

def fetch(ctx, id:)
resp = Helpers::PartnersAPI.query(ctx, 'find_organization', id: id)
resp = ShopifyCli::PartnersAPI.query(ctx, 'find_organization', id: id)
org = resp['data']['organizations']['nodes'].first
return nil if org.nil?
org['stores'] = org['stores']['nodes']
org
end

def fetch_with_app(ctx)
resp = Helpers::PartnersAPI.query(ctx, 'all_orgs_with_apps')
resp = ShopifyCli::PartnersAPI.query(ctx, 'all_orgs_with_apps')
resp['data']['organizations']['nodes'].map do |org|
org['stores'] = org['stores']['nodes']
org['apps'] = org['apps']['nodes']
Expand Down
64 changes: 0 additions & 64 deletions lib/shopify-cli/helpers/partners_api.rb

This file was deleted.

62 changes: 62 additions & 0 deletions lib/shopify-cli/partners_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'shopify_cli'

module ShopifyCli
class PartnersAPI < API
ENV_VAR = 'SHOPIFY_APP_CLI_LOCAL_PARTNERS'
AUTH_PROD_URI = 'https://accounts.shopify.com'
AUTH_DEV_URI = 'https://identity.myshopify.io'
PROD_URI = 'https://partners.shopify.com'
DEV_URI = 'https://partners.myshopify.io/'
PROD_ID = '271e16d403dfa18082ffb3d197bd2b5f4479c3fc32736d69296829cbb28d41a6'
DEV_ID = 'df89d73339ac3c6c5f0a98d9ca93260763e384d51d6038da129889c308973978'
PROD_CLI_ID = 'fbdb2649-e327-4907-8f67-908d24cfd7e3'
DEV_CLI_ID = 'e5380e02-312a-7408-5718-e07017e9cf52'

class << self
def id
ENV[ENV_VAR].nil? ? PROD_ID : DEV_ID
end

def cli_id
ENV[ENV_VAR].nil? ? PROD_CLI_ID : DEV_CLI_ID
end

def auth_endpoint
ENV[ENV_VAR].nil? ? AUTH_PROD_URI : AUTH_DEV_URI
end

def endpoint
ENV[ENV_VAR].nil? ? PROD_URI : DEV_URI
end

def query(ctx, body, **variables)
authenticated_req(ctx) do
api_client(ctx).query(body, variables: variables)
end
end

private

def authenticated_req(ctx)
yield
rescue API::APIRequestUnauthorizedError
Tasks::AuthenticateIdentity.call(ctx)
retry
rescue API::APIRequestNotFoundError
ctx.puts("{{x}} error: Your account was not found. Please sign up at https://partners.shopify.com/signup")
end

def api_client(ctx)
new(
ctx: ctx,
token: Helpers::PkceToken.read(ctx),
url: "#{endpoint}/api/cli/graphql",
)
end
end

def auth_headers(token)
{ Authorization: "Bearer #{token}" }
end
end
end
6 changes: 3 additions & 3 deletions lib/shopify-cli/tasks/authenticate_identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def call(ctx)
OAuth.new(
ctx: ctx,
service: 'identity',
client_id: Helpers::PartnersAPI.cli_id,
client_id: ShopifyCli::PartnersAPI.cli_id,
scopes: SCOPES,
request_exchange: Helpers::PartnersAPI.id,
).authenticate("#{Helpers::PartnersAPI.auth_endpoint}/oauth")
request_exchange: ShopifyCli::PartnersAPI.id,
).authenticate("#{ShopifyCli::PartnersAPI.auth_endpoint}/oauth")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify-cli/tasks/create_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CreateApiClient < ShopifyCli::Task
VALID_APP_TYPES = %w(public custom)

def call(ctx, org_id:, title:, app_url:, type:)
resp = Helpers::PartnersAPI.query(
resp = ShopifyCli::PartnersAPI.query(
ctx,
'create_app',
org: org_id.to_i,
Expand Down
4 changes: 2 additions & 2 deletions lib/shopify-cli/tasks/ensure_loopback_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class EnsureLoopbackURL < ShopifyCli::Task
def call(ctx)
@ctx = ctx
api_key = Project.current.env.api_key
result = Helpers::PartnersAPI.query(ctx, 'get_app_urls', apiKey: api_key)
result = ShopifyCli::PartnersAPI.query(ctx, 'get_app_urls', apiKey: api_key)
loopback = OAuth::REDIRECT_HOST
app = result['data']['app']
urls = app['redirectUrlWhitelist']
if urls.grep(/#{loopback}/).empty?
with_loopback = urls.push(loopback)
ShopifyCli::Helpers::PartnersAPI.query(@ctx, 'update_dashboard_urls', input: {
ShopifyCli::PartnersAPI.query(@ctx, 'update_dashboard_urls', input: {
redirectUrlWhitelist: with_loopback, apiKey: api_key
})
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify-cli/tasks/ensure_test_shop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call(ctx)
return if shop['transferDisabled'] == true
return unless CLI::UI::Prompt.confirm("Do you want to convert #{project.env.shop} to a test shop."\
" This will enable you to install your app on this store.")
ShopifyCli::Helpers::PartnersAPI.query(ctx, 'convert_dev_to_test_store', input: {
ShopifyCli::PartnersAPI.query(ctx, 'convert_dev_to_test_store', input: {
organizationID: shop['orgID'].to_i,
shopId: shop['shopId'],
})
Expand Down
4 changes: 2 additions & 2 deletions lib/shopify-cli/tasks/update_dashboard_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def call(ctx, url:, callback_url:)
@ctx = ctx
project = ShopifyCli::Project.current
api_key = project.env.api_key
result = Helpers::PartnersAPI.query(ctx, 'get_app_urls', apiKey: api_key)
result = ShopifyCli::PartnersAPI.query(ctx, 'get_app_urls', apiKey: api_key)
app = result['data']['app']
consent = check_application_url(app['applicationUrl'], url)
constructed_urls = construct_redirect_urls(app['redirectUrlWhitelist'], url, callback_url)
return if url == app['applicationUrl']
ShopifyCli::Helpers::PartnersAPI.query(@ctx, 'update_dashboard_urls', input: {
ShopifyCli::PartnersAPI.query(@ctx, 'update_dashboard_urls', input: {
applicationUrl: consent ? url : app['applicationUrl'],
redirectUrlWhitelist: constructed_urls, apiKey: api_key
})
Expand Down
1 change: 1 addition & 0 deletions lib/shopify_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ module ShopifyCli
autoload :Log, 'shopify-cli/log'
autoload :OAuth, 'shopify-cli/oauth'
autoload :Options, 'shopify-cli/options'
autoload :PartnersAPI, 'shopify-cli/partners_api'
autoload :ProcessSupervision, 'shopify-cli/process_supervision'
autoload :Project, 'shopify-cli/project'
autoload :ProjectType, 'shopify-cli/project_type'
Expand Down
2 changes: 1 addition & 1 deletion test/shopify-cli/commands/connect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_run
}],
}]
ShopifyCli::Helpers::Organizations.stubs(:fetch_with_app).returns(response)
CLI::UI::Prompt.expects(:ask).with('Which organization does this project belong to?').returns(422)
CLI::UI::Prompt.expects(:ask).with('To which organization does this project belong?').returns(422)
CLI::UI::Prompt.expects(:ask).with(
'Which development store would you like to use?'
).returns('store.myshopify.com')
Expand Down
94 changes: 0 additions & 94 deletions test/shopify-cli/helpers/partners_api_test.rb

This file was deleted.

Loading

0 comments on commit 1a7c267

Please sign in to comment.