Class: Supply::Client

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

Instance Attribute Summary collapse

Login collapse

Handling the edit lifecycle collapse

Getting data collapse

Modifying data collapse

Screenshots collapse

Constructor Details

#initialize(path_to_key: nil, issuer: nil, passphrase: nil) ⇒ Client

Initializes the auth_client and api_client using the specified information

Parameters:

  • path_to_key: (defaults to: nil)

    The path to your p12 file

  • issuer: (defaults to: nil)

    Email addresss for oauth

  • passphrase: (defaults to: nil)

    Passphrase for the p12 file



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
# File 'lib/supply/client.rb', line 25

def initialize(path_to_key: nil, issuer: nil, passphrase: nil)
  passphrase ||= "notasecret"

  key = Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(path_to_key), passphrase)

  begin
    self.auth_client = Signet::OAuth2::Client.new(
      token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
      audience: 'https://accounts.google.com/o/oauth2/token',
      scope: 'https://www.googleapis.com/auth/androidpublisher',
      issuer: issuer,
      signing_key: key
    )
  rescue => ex
    Helper.log.fatal ex
    raise "Authentification unsuccessful, make sure to pass a valid key file".red
  end

  Helper.log.debug "Fetching a new access token from Google..."

  self.auth_client.fetch_access_token!

  self.api_client = Google::APIClient.new(
    application_name: "fastlane - supply",
    application_version: Supply::VERSION
  )

  self.android_publisher = api_client.discovered_api('androidpublisher', 'v2')
end

Instance Attribute Details

#android_publisherObject

Returns the value of attribute android_publisher.



9
10
11
# File 'lib/supply/client.rb', line 9

def android_publisher
  @android_publisher
end

#api_clientObject

Returns the value of attribute api_client.



8
9
10
# File 'lib/supply/client.rb', line 8

def api_client
  @api_client
end

#auth_clientObject

Connecting with Google



7
8
9
# File 'lib/supply/client.rb', line 7

def auth_client
  @auth_client
end

#current_editObject

Editing something Reference to the entry we’re currently editing. Might be nil if don’t have one open



13
14
15
# File 'lib/supply/client.rb', line 13

def current_edit
  @current_edit
end

#current_package_nameObject

Package name of the currently edited element



15
16
17
# File 'lib/supply/client.rb', line 15

def current_package_name
  @current_package_name
end

Instance Method Details

#abort_current_editObject

Aborts the current edit deleting all pending changes



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/supply/client.rb', line 79

def abort_current_edit
  ensure_active_edit!

  result = api_client.execute(
    api_method: android_publisher.edits.delete,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name
    },
    authorization: auth_client
  )

  raise result.error_message.red if result.error?

  self.current_edit = nil
  self.current_package_name = nil
end

#begin_edit(package_name: nil) ⇒ Object

Begin modifying a certain package



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/supply/client.rb', line 60

def begin_edit(package_name: nil)
  raise "You currently have an active edit" if @current_edit

  self.current_edit = api_client.execute(
    api_method: android_publisher.edits.insert,
    parameters: { 'packageName' => package_name },
    authorization: auth_client
  )

  if current_edit.error?
    error_message = current_edit.error_message
    self.current_edit = nil
    raise error_message
  end

  self.current_package_name = package_name
end

#clear_screenshots(image_type: nil, language: nil) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/supply/client.rb', line 278

def clear_screenshots(image_type: nil, language: nil)
  ensure_active_edit!

  result = @api_client.execute(
    api_method: @android_publisher.edits.images.deleteall,
    parameters: {
        'editId' => current_edit.data.id,
        'packageName' => current_package_name,
        'language' => language,
        'imageType' => image_type
      },
    authorization: auth_client
  )

  raise result.error_message if result.error?
end

#commit_current_edit!Object

Commits the current edit saving all pending changes on Google Play



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/supply/client.rb', line 98

def commit_current_edit!
  ensure_active_edit!

  result = api_client.execute(
    api_method: android_publisher.edits.commit,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name
    },
    authorization: auth_client
  )

  raise result.error_message.red if result.error?

  self.current_edit = nil
  self.current_package_name = nil
end

#fetch_images(image_type: nil, language: nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/supply/client.rb', line 238

def fetch_images(image_type: nil, language: nil)
  ensure_active_edit!

  result = api_client.execute(
    api_method: android_publisher.edits.images.list,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name,
      'language' => language,
      'imageType' => image_type
    },
    authorization: auth_client
  )

  raise result.error_message.red if result.error?

  result.data.images.collect(&:url)
end

#listing_for_language(language) ⇒ Object

Returns the listing for the given language filled with the current values if it already exists



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/supply/client.rb', line 142

def listing_for_language(language)
  ensure_active_edit!

  result = api_client.execute(
    api_method: android_publisher.edits.listings.get,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name,
      'language' => language
    },
    authorization: auth_client
  )

  raise result.error_message.red if result.error? && result.status != 404

  if result.status == 404
    return Listing.new(self, language) # create a new empty listing
  else
    return Listing.new(self, language, result.data)
  end
end

#listingsObject

Get a list of all languages - returns the list make sure to have an active edit



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/supply/client.rb', line 122

def listings
  ensure_active_edit!

  result = api_client.execute(
    api_method: android_publisher.edits.listings.list,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name
    },
    authorization: auth_client
  )

  raise result.error_message.red if result.error? && result.status != 404

  return result.data.listings.collect do |row|
    Listing.new(self, row.language, row)
  end
end

#update_listing_for_language(language: nil, title: nil, short_description: nil, full_description: nil, video: nil) ⇒ Object

Updates or creates the listing for the specified language



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/supply/client.rb', line 169

def update_listing_for_language(language: nil, title: nil, short_description: nil, full_description: nil, video: nil)
  ensure_active_edit!

  listing = {
    'language' => language,
    'title' => title,
    'fullDescription' => full_description,
    'shortDescription' => short_description,
    'video' => video
  }

  result = api_client.execute(
    api_method: android_publisher.edits.listings.update,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name,
      'language' => language
    },
    body_object: listing,
    authorization: auth_client
  )
  raise result.error_message.red if result.error?
end

#upload_apk_to_track(path_to_apk, track) ⇒ Object



193
194
195
# File 'lib/supply/client.rb', line 193

def upload_apk_to_track(path_to_apk, track)
  upload_apk_to_track_with_rollout(path_to_apk, track, 1.0)
end

#upload_apk_to_track_with_rollout(path_to_apk, track, rollout) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/supply/client.rb', line 197

def upload_apk_to_track_with_rollout(path_to_apk, track, rollout)
  ensure_active_edit!

  apk = Google::APIClient::UploadIO.new(File.expand_path(path_to_apk), 'application/vnd.android.package-archive')
  result_upload = api_client.execute(
    api_method: android_publisher.edits.apks.upload,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name,
      'uploadType' => 'media'
    },
    media: apk,
    authorization: auth_client
  )

  raise result_upload.error_message.red if result_upload.error?

  track_body = {
    'track' => track,
    'userFraction' => rollout,
    'versionCodes' => [result_upload.data.versionCode]
  }

  result_update = api_client.execute(
    api_method: android_publisher.edits.tracks.update,
    parameters:
      {
        'editId' => current_edit.data.id,
        'packageName' => current_package_name,
        'track' => track
      },
    body_object: track_body,
    authorization: auth_client)

  raise result_update.error_message.red if result_update.error?
end

#upload_image(image_path: nil, image_type: nil, language: nil) ⇒ Object

Parameters:

  • image_type (e.g. phoneScreenshots, sevenInchScreenshots, ...) (defaults to: nil)


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/supply/client.rb', line 258

def upload_image(image_path: nil, image_type: nil, language: nil)
  ensure_active_edit!

  image = Google::APIClient::UploadIO.new(image_path, 'image/*')
  result = api_client.execute(
    api_method: android_publisher.edits.images.upload,
    parameters: {
      'editId' => current_edit.data.id,
      'packageName' => current_package_name,
      'language' => language,
      'imageType' => image_type,
      'uploadType' => 'media'
    },
    media: image,
    authorization: auth_client
  )

  raise result.error_message.red if result.error?
end