Class: Spaceship::ConnectAPI::APIClient

Inherits:
Spaceship::Client show all
Defined in:
spaceship/lib/spaceship/connect_api/api_client.rb

Defined Under Namespace

Classes: TimeoutRetryError

Constant Summary

Constants inherited from Spaceship::Client

Spaceship::Client::AUTH_TYPES, Spaceship::Client::AccessForbiddenError, Spaceship::Client::AppleTimeoutError, Spaceship::Client::BadGatewayError, Spaceship::Client::BasicPreferredInfoError, Spaceship::Client::GatewayTimeoutError, Spaceship::Client::InsufficientPermissions, Spaceship::Client::InternalServerError, Spaceship::Client::InvalidUserCredentialsError, Spaceship::Client::NoUserCredentialsError, Spaceship::Client::PROTOCOL_VERSION, Spaceship::Client::ProgramLicenseAgreementUpdated, Spaceship::Client::TooManyRequestsError, Spaceship::Client::USER_AGENT, Spaceship::Client::UnauthorizedAccessError, Spaceship::Client::UnexpectedResponse

Instance Attribute Summary collapse

Attributes inherited from Spaceship::Client

#additional_headers, #client, #csrf_tokens, #logger, #provider, #user, #user_email

Client Init collapse

Methods inherited from Spaceship::Client

#UI, #ask_for_2fa_code, #choose_phone_number, client_with_authorization_from, #cookie, #detect_most_common_errors_and_raise_exceptions, #exit_with_session_state, #fastlane_user_dir, #fetch_hashcash, #fetch_olympus_session, #fetch_program_license_agreement_messages, #handle_two_factor, #handle_two_step, #handle_two_step_for_device, #handle_two_step_or_factor, #has_valid_session, #itc_service_key, #load_session_from_env, #load_session_from_file, #login, login, #match_phone_to_masked_phone, #page_size, #paging, #parse_response, #persistent_cookie_path, #phone_id_from_masked_number, #phone_id_from_number, #push_mode_from_masked_number, #push_mode_from_number, #raise_insufficient_permission_error!, #request, #request_two_factor_code_from_phone, #request_two_factor_code_from_phone_choose, #send_shared_login_request, #sms_automatically_sent, #sms_fallback, spaceship_session_env, #store_cookie, #store_session, #team_id, #team_id=, #team_information, #team_name, #teams, #try_upgrade_2fa_later, #update_request_headers, #user_details_data, #with_retry

Constructor Details

#initialize(cookie: nil, current_team_id: nil, token: nil, csrf_tokens: nil, another_client: nil) ⇒ APIClient

Instantiates a client with cookie session or a JWT token.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 20

def initialize(cookie: nil, current_team_id: nil, token: nil, csrf_tokens: nil, another_client: nil)
  params_count = [cookie, token, another_client].compact.size
  if params_count != 1
    raise "Must initialize with one of :cookie, :token, or :another_client"
  end

  if token.nil?
    if another_client.nil?
      super(cookie: cookie, current_team_id: current_team_id, csrf_tokens: csrf_tokens, timeout: 1200)
      return
    end
    super(cookie: another_client.instance_variable_get(:@cookie), current_team_id: another_client.team_id, csrf_tokens: another_client.csrf_tokens)
  else
    options = {
      request: {
        timeout:       (ENV["SPACESHIP_TIMEOUT"] || 300).to_i,
        open_timeout:  (ENV["SPACESHIP_TIMEOUT"] || 300).to_i
      }
    }
    @token = token
    @current_team_id = current_team_id

    @client = Faraday.new(hostname, options) do |c|
      c.response(:json, content_type: /\bjson$/)
      c.response(:plist, content_type: /\bplist$/)
      c.use(FaradayMiddleware::RelsMiddleware)
      c.use(Spaceship::StatsMiddleware)
      c.use(Spaceship::TokenRefreshMiddleware, token)
      c.adapter(Faraday.default_adapter)

      if ENV['SPACESHIP_DEBUG']
        # for debugging only
        # This enables tracking of networking requests using Charles Web Proxy
        c.proxy = "https://127.0.0.1:8888"
        c.ssl[:verify_mode] = OpenSSL::SSL::VERIFY_NONE
      elsif ENV["SPACESHIP_PROXY"]
        c.proxy = ENV["SPACESHIP_PROXY"]
        c.ssl[:verify_mode] = OpenSSL::SSL::VERIFY_NONE if ENV["SPACESHIP_PROXY_SSL_VERIFY_NONE"]
      end

      if ENV["DEBUG"]
        puts("To run spaceship through a local proxy, use SPACESHIP_DEBUG")
      end
    end
  end
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 13

def token
  @token
end

Class Method Details

.hostnameObject



77
78
79
80
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 77

def self.hostname
  # Implemented in subclass
  not_implemented(__method__)
end

Instance Method Details

#build_params(filter: nil, includes: nil, limit: nil, sort: nil, cursor: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 90

def build_params(filter: nil, includes: nil, limit: nil, sort: nil, cursor: nil)
  params = {}

  filter = filter.delete_if { |k, v| v.nil? } if filter

  params[:filter] = filter if filter && !filter.empty?
  params[:include] = includes if includes
  params[:limit] = limit if limit
  params[:sort] = sort if sort
  params[:cursor] = cursor if cursor

  return params
end

#delete(url_or_path, params = nil, body = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 138

def delete(url_or_path, params = nil, body = nil)
  response = with_asc_retry do
    request(:delete) do |req|
      req.url(url_or_path)
      req.options.params_encoder = Faraday::NestedParamsEncoder if params
      req.params = params if params
      req.body = body.to_json if body
      req.headers['Content-Type'] = 'application/json' if body
    end
  end
  handle_response(response)
end

#get(url_or_path, params = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 104

def get(url_or_path, params = nil)
  response = with_asc_retry do
    request(:get) do |req|
      req.url(url_or_path)
      req.options.params_encoder = Faraday::NestedParamsEncoder
      req.params = params if params
      req.headers['Content-Type'] = 'application/json'
    end
  end
  handle_response(response)
end

#hostnameObject

Instance level hostname only used when creating App Store Connect API Farady client. Forwarding to class level if using web session.



70
71
72
73
74
75
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 70

def hostname
  if @token
    return "https://api.appstoreconnect.apple.com/v1/"
  end
  return self.class.hostname
end

#patch(url_or_path, body) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 127

def patch(url_or_path, body)
  response = with_asc_retry do
    request(:patch) do |req|
      req.url(url_or_path)
      req.body = body.to_json
      req.headers['Content-Type'] = 'application/json'
    end
  end
  handle_response(response)
end

#post(url_or_path, body, tries: 5) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 116

def post(url_or_path, body, tries: 5)
  response = with_asc_retry(tries) do
    request(:post) do |req|
      req.url(url_or_path)
      req.body = body.to_json
      req.headers['Content-Type'] = 'application/json'
    end
  end
  handle_response(response)
end

#web_session?Boolean

Helpers

Returns:

  • (Boolean)


86
87
88
# File 'spaceship/lib/spaceship/connect_api/api_client.rb', line 86

def web_session?
  return @token.nil?
end