Class: Spaceship::ConnectAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
spaceship/lib/spaceship/connect_api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookie: nil, current_team_id: nil, token: nil, tunes_client: nil, portal_client: nil) ⇒ Client

Returns a new instance of Client.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 72

def initialize(cookie: nil, current_team_id: nil, token: nil, tunes_client: nil, portal_client: nil)
  @token = token

  # If using web session...
  # Spaceship::Tunes is needed for TestFlight::API, Tunes::API, and Users::API
  # Spaceship::Portal is needed for Provisioning::API
  @tunes_client = tunes_client
  @portal_client = portal_client

  # Extending this instance to add API endpoints from these modules
  # Each of these modules adds a new setter method for an instance
  # of an ConnectAPI::APIClient
  # These get set in set_indvidual_clients
  self.extend(Spaceship::ConnectAPI::TestFlight::API)
  self.extend(Spaceship::ConnectAPI::Tunes::API)
  self.extend(Spaceship::ConnectAPI::Provisioning::API)
  self.extend(Spaceship::ConnectAPI::Users::API)

  set_indvidual_clients(
    cookie: cookie,
    current_team_id: current_team_id,
    token: token,
    tunes_client: @tunes_client,
    portal_client: @portal_client
  )
end

Instance Attribute Details

#portal_clientObject

Returns the value of attribute portal_client.



12
13
14
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 12

def portal_client
  @portal_client
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 10

def token
  @token
end

#tunes_clientObject

Returns the value of attribute tunes_client.



11
12
13
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 11

def tunes_client
  @tunes_client
end

Class Method Details

.auth(key_id: nil, issuer_id: nil, filepath: nil) ⇒ Spaceship::ConnectAPI::Client

Initializes client with Apple’s App Store Connect JWT auth key.

This method will automatically use the key id, issuer id, and filepath from environment variables if not given.

All three parameters are needed to authenticate.

Parameters:

  • key_id (String) (defaults to: nil)

    (optional): The key id

  • issuer_id (String) (defaults to: nil)

    (optional): The issuer id

  • filepath (String) (defaults to: nil)

    (optional): The filepath

Returns:

Raises:

  • InvalidUserCredentialsError: raised if authentication failed



28
29
30
31
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 28

def self.auth(key_id: nil, issuer_id: nil, filepath: nil)
  token = Spaceship::ConnectAPI::Token.create(key_id: key_id, issuer_id: issuer_id, filepath: filepath)
  return ConnectAPI::Client.new(token: token)
end

.login(user = nil, password = nil, use_portal: true, use_tunes: true, portal_team_id: nil, tunes_team_id: nil, team_name: nil, skip_select_team: false) ⇒ Spaceship::ConnectAPI::Client

Authenticates with Apple’s web services. This method has to be called once to generate a valid session.

This method will automatically use the username from the Appfile (if available) and fetch the password from the Keychain (if available)

Parameters:

  • user (String) (defaults to: nil)

    (optional): The username (usually the email address)

  • password (String) (defaults to: nil)

    (optional): The password

  • use_portal (Boolean) (defaults to: true)

    (optional): Whether to log in to Spaceship::Portal or not

  • use_tunes (Boolean) (defaults to: true)

    (optional): Whether to log in to Spaceship::Tunes or not

  • portal_team_id (String) (defaults to: nil)

    (optional): The Spaceship::Portal team id

  • tunes_team_id (String) (defaults to: nil)

    (optional): The Spaceship::Tunes team id

  • team_name (String) (defaults to: nil)

    (optional): The team name

  • skip_select_team (Boolean) (defaults to: false)

    (optional): Whether to skip automatic selection or prompt for team

Returns:

Raises:

  • InvalidUserCredentialsError: raised if authentication failed



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 51

def self.(user = nil, password = nil, use_portal: true, use_tunes: true, portal_team_id: nil, tunes_team_id: nil, team_name: nil, skip_select_team: false)
  portal_client = Spaceship::Portal.(user, password) if use_portal
  tunes_client = Spaceship::Tunes.(user, password) if use_tunes

  unless skip_select_team
    # Check if environment variables are set for Spaceship::Portal or Spaceship::Tunes to select team
    portal_team_id ||= ENV['FASTLANE_TEAM_ID']
    portal_team_name = team_name || ENV['FASTLANE_TEAM_NAME']
    tunes_team_id ||= ENV['FASTLANE_ITC_TEAM_ID']
    tunes_team_name = team_name || ENV['FASTLANE_ITC_TEAM_NAME']

    # The clients will prompt for a team selection if:
    # 1. client exists
    # 2. team_id and team_name are nil and user belongs to multiple teams
    portal_client.select_team(team_id: portal_team_id, team_name: portal_team_name) if portal_client
    tunes_client.select_team(team_id: tunes_team_id, team_name: tunes_team_name) if tunes_client
  end

  return ConnectAPI::Client.new(tunes_client: tunes_client, portal_client: portal_client)
end

Instance Method Details

#in_house?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 119

def in_house?
  if token
    if token.in_house.nil?
      message = [
        "Cannot determine if team is App Store or Enterprise via the App Store Connect API (yet)",
        "Set 'in_house' on your Spaceship::ConnectAPI::Token",
        "Or set 'in_house' in your App Store Connect API key JSON file",
        "Or set the 'SPACESHIP_CONNECT_API_IN_HOUSE' environment variable to 'true'",
        "View more info in the docs at https://docs.fastlane.tools/app-store-connect-api/"
      ]
      raise message.join('\n')
    end
    return !!token.in_house
  elsif @portal_client
    return @portal_client.in_house?
  else
    raise "No App Store Connect API token or Portal Client set"
  end
end

#portal_team_idObject



99
100
101
102
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 99

def portal_team_id
  return nil if @portal_client.nil?
  return @portal_client.team_id
end

#portal_teamsObject



109
110
111
112
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 109

def portal_teams
  return nil if @portal_client.nil?
  return @portal_client.teams
end

#select_team(portal_team_id: nil, tunes_team_id: nil, team_name: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 139

def select_team(portal_team_id: nil, tunes_team_id: nil, team_name: nil)
  @portal_client.select_team(team_id: portal_team_id, team_name: team_name) unless @portal_client.nil?
  @tunes_client.select_team(team_id: tunes_team_id, team_name: team_name) unless @tunes_client.nil?

  # Updating the tunes and portal clients requires resetting
  # of the clients in the API modules
  set_indvidual_clients(
    cookie: nil,
    current_team_id: nil,
    token: nil,
    tunes_client: tunes_client,
    portal_client: portal_client
  )
end

#tunes_team_idObject



104
105
106
107
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 104

def tunes_team_id
  return nil if @tunes_client.nil?
  return @tunes_client.team_id
end

#tunes_teamsObject



114
115
116
117
# File 'spaceship/lib/spaceship/connect_api/client.rb', line 114

def tunes_teams
  return nil if @tunes_client.nil?
  return @tunes_client.teams
end