This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from qiwi/develop
Add last updates
- Loading branch information
Showing
10 changed files
with
157 additions
and
294 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
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 |
---|---|---|
|
@@ -9,4 +9,5 @@ class AndroidPublishPluginExtension { | |
String keyPath | ||
String settingsPath | ||
String track | ||
String updateMessage | ||
} |
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,52 @@ | ||
package com.qiwi.mobile | ||
import com.google.api.services.androidpublisher.AndroidPublisher | ||
import com.google.api.services.androidpublisher.model.Apk | ||
import com.google.api.services.androidpublisher.model.ApkListing | ||
import com.google.api.services.androidpublisher.model.AppEdit | ||
import com.google.api.services.androidpublisher.model.Track | ||
|
||
class Publisher { | ||
|
||
static boolean upload(AndroidPublishPluginExtension params) { | ||
try { | ||
AndroidPublisher service = PublisherHelper.init(params) | ||
final AndroidPublisher.Edits edits = service.edits() | ||
|
||
AndroidPublisher.Edits.Insert editRequest = edits | ||
.insert(params.configPackageName, null) | ||
AppEdit edit = editRequest.execute() | ||
final String editId = edit.getId() | ||
|
||
AndroidPublisher.Edits.Apks.Upload uploadRequest = edits | ||
.apks() | ||
.upload(params.configPackageName, editId, PublisherHelper.getApk(params.configApkPath)) | ||
Apk apk = uploadRequest.execute() | ||
|
||
List<Integer> apkVersionCodes = new ArrayList<>() | ||
apkVersionCodes.add(apk.getVersionCode()) | ||
AndroidPublisher.Edits.Tracks.Update updateTrackRequest = edits | ||
.tracks() | ||
.update(params.configPackageName, editId, params.track, new Track().setVersionCodes(apkVersionCodes)) | ||
updateTrackRequest.execute() | ||
|
||
if (params.updateMessage != "" && params.updateMessage != null) { | ||
def json = PublisherHelper.parseJson(params.updateMessage) | ||
json.messages.each { entry -> | ||
AndroidPublisher.Edits.Apklistings.Update updateRecentChangesRequest = edits | ||
.apklistings() | ||
.update(params.configPackageName, editId, apk.getVersionCode(), | ||
entry.locale, new ApkListing().setRecentChanges(entry.text)) | ||
updateRecentChangesRequest.execute() | ||
} | ||
} | ||
|
||
AndroidPublisher.Edits.Commit commitRequest = edits.commit(params.configPackageName, editId) | ||
commitRequest.execute() | ||
true | ||
} catch (Exception e) { | ||
println("Exception was thrown while uploading apk: " + e) | ||
false | ||
} | ||
} | ||
|
||
} |
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,80 @@ | ||
package com.qiwi.mobile | ||
|
||
import com.google.api.client.auth.oauth2.Credential | ||
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp | ||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport | ||
import com.google.api.client.http.AbstractInputStreamContent | ||
import com.google.api.client.http.FileContent | ||
import com.google.api.client.http.HttpTransport | ||
import com.google.api.client.json.JsonFactory | ||
import com.google.api.client.json.jackson2.JacksonFactory | ||
import com.google.api.client.util.store.FileDataStoreFactory | ||
import com.google.api.services.androidpublisher.AndroidPublisher | ||
import com.google.api.services.androidpublisher.AndroidPublisherScopes | ||
import groovy.json.JsonSlurper | ||
|
||
class PublisherHelper { | ||
|
||
private static final String MIME_TYPE_APK = "application/vnd.android.package-archive" | ||
private static final String DATA_STORE_SYSTEM_PROPERTY = "user.home" | ||
private static final String DATA_STORE_FILE = ".store/android_publisher_api" | ||
private static final File DATA_STORE_DIR = | ||
new File(System.getProperty(DATA_STORE_SYSTEM_PROPERTY), DATA_STORE_FILE) | ||
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance() | ||
private static HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport() | ||
private static final String INST_APP_USER_ID = "user" | ||
private static FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR) | ||
|
||
static AndroidPublisher init(AndroidPublishPluginExtension params) { | ||
Credential credential | ||
if (params.configEmail == null || params.configEmail.isEmpty()) { | ||
credential = authorizeWithInstalledApplication(params) | ||
} else { | ||
credential = authorizeWithServiceAccount(params) | ||
} | ||
|
||
new AndroidPublisher.Builder( | ||
HTTP_TRANSPORT, JSON_FACTORY, credential) | ||
.setApplicationName(params.configAppName) | ||
.build() | ||
} | ||
|
||
static Credential authorizeWithServiceAccount(AndroidPublishPluginExtension params) { | ||
new GoogleCredential.Builder() | ||
.setTransport(HTTP_TRANSPORT) | ||
.setJsonFactory(JSON_FACTORY) | ||
.setServiceAccountId(params.configEmail) | ||
.setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)) | ||
.setServiceAccountPrivateKeyFromP12File(new File(params.keyPath)) | ||
.build() | ||
} | ||
|
||
static Credential authorizeWithInstalledApplication(AndroidPublishPluginExtension params) { | ||
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( | ||
JSON_FACTORY, new InputStreamReader(PublisherHelper.class.getResourceAsStream(params.settingsPath))) | ||
|
||
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( | ||
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, | ||
Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)) | ||
.setDataStoreFactory(dataStoreFactory) | ||
.build() | ||
|
||
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) | ||
.authorize(INST_APP_USER_ID) | ||
} | ||
|
||
static AbstractInputStreamContent getApk(String path) { | ||
new FileContent(MIME_TYPE_APK, new File(path)) | ||
} | ||
|
||
static Object parseJson(String path) { | ||
File file = new File(path) | ||
JsonSlurper parser = new JsonSlurper() | ||
parser.parse(new FileReader(file)) | ||
} | ||
|
||
} |
Oops, something went wrong.