Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Maipulating Bintray API with Groovy
@Grab( 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2' )
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
def repoOwner = 'ysb33r'
def repoName = 'grysb33r'
def packageName = 'bintray-gradle-plugin'
def bintrayUsername = '__YOUR_USERNAME__'
def bintrayApiKey = '__YOUR_APIKEY__'
def packageVersion = '0.1'
def rest = new RESTClient("https://api.bintray.com/packages/${repoOwner}/${repoName}/${packageName}/")
rest.auth.basic bintrayUsername,bintrayApiKey
// ===============================================
// Let's try to see if the specific version exists
// ===============================================
try {
def response = rest.get( path : "versions/${packageVersion}" )
// To get the status
println response.status
// To get the JSON response
println response.data
// Was it a success (shortcut) ?
println response.isSuccess()
} catch (HttpResponseException e) {
// OK, so this might throw an exception
println e.response.status
// Reason for failure
println e.message
// Response code of 404 means version does not exist,
// That is expected behaviour, you may want to create it
// after this point
// In all other cases re-throw
if(e.response.status != 404) {
throw e
}
}
// ============================================================================
// Deleting a version is similar to the above, just replace 'get' with 'delete'
// Use of exceptions etc. are all the same
// ============================================================================
def deleteResponse = rest.delete( path : "versions/${packageVersion}" )
// ====================================
// To create a new version of a package
// ====================================
def response = rest.post(
contentType : JSON,
path : "versions",
body : [ name : packageVersion, desc : '__A_DECRIPTION_IS_THIS_VERSION_OF_THE_PACKAGE__' ]
)
// To get the status - Expect a 201 as per Bintray API.
// (At this point you are ready to upload using maven-style publishing)
// In case of failure, exception logic applies as above
assert response.status == 201
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment