|
| 1 | +package publish |
| 2 | + |
| 3 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential |
| 4 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport |
| 5 | +import com.google.api.client.json.jackson2.JacksonFactory |
| 6 | +import com.google.api.services.androidpublisher.AndroidPublisher |
| 7 | +import com.google.api.services.androidpublisher.AndroidPublisherScopes |
| 8 | +import java.io.File |
| 9 | + |
| 10 | +fun androidPublisher(packageName: String, serviceAccountJson: File): AndroidPublisher { |
| 11 | + val credential = serviceAccountJson.asCredential() |
| 12 | + |
| 13 | + return AndroidPublisher.Builder( |
| 14 | + GoogleNetHttpTransport.newTrustedTransport(), |
| 15 | + JacksonFactory.getDefaultInstance(), |
| 16 | + credential |
| 17 | + ) |
| 18 | + .setApplicationName(packageName) |
| 19 | + .build() |
| 20 | +} |
| 21 | + |
| 22 | +fun File.asCredential(): GoogleCredential { |
| 23 | + if (!exists()) { |
| 24 | + error("$this does not exist") |
| 25 | + } |
| 26 | + |
| 27 | + return inputStream().use { credentialStream -> |
| 28 | + GoogleCredential.fromStream(credentialStream) |
| 29 | + .createScoped( |
| 30 | + listOf(AndroidPublisherScopes.ANDROIDPUBLISHER) |
| 31 | + ) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fun AndroidPublisher.Edits.runInTransaction( |
| 36 | + packageName: String, |
| 37 | + editId: String = insert(packageName, null).execute().id, |
| 38 | + action: (editId: String) -> Unit = {}, |
| 39 | + errorHandler: (error: Throwable) -> Unit = {} |
| 40 | +) { |
| 41 | + try { |
| 42 | + action(editId) |
| 43 | + |
| 44 | + commit(packageName, editId).execute() |
| 45 | + } catch (th: Throwable) { |
| 46 | + errorHandler(th) |
| 47 | + } |
| 48 | +} |
| 49 | + |
0 commit comments