Last active
October 26, 2015 19:05
-
-
Save aaronzirbes/3685795dbd9f1abcdcc5 to your computer and use it in GitHub Desktop.
Starter script for Groovy scripts that call APIs and do stuff.
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
@Grapes([ | |
@Grab(group='com.squareup.retrofit', module='retrofit', version='2.0.0-beta2'), | |
@Grab(group='com.squareup.retrofit', module='converter-jackson', version='2.0.0-beta2'), | |
@Grab(group='com.fasterxml.jackson.core', module='jackson-core', version='2.6.3'), | |
@Grab(group='com.fasterxml.jackson.core', module='jackson-databind', version='2.6.3'), | |
@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.12') | |
]) | |
import retrofit.Retrofit | |
import retrofit.Call | |
import retrofit.Response | |
import retrofit.http.* | |
import retrofit.JacksonConverterFactory | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import groovy.util.logging.Slf4j | |
@Slf4j | |
class GroovyApiScript { | |
static void main(String ... args) { | |
log.info 'Running script.' | |
new ScriptRunner().go() | |
log.info "Finished running script." | |
} | |
} | |
@Slf4j | |
protected class ScriptRunner { | |
final ObjectMapper objectMapper | |
ScriptRunner() { | |
objectMapper = new ObjectMapper() | |
} | |
void go() { | |
Map<String, Integer> vehicleSizes = [:] | |
GitHub api = new Retrofit.Builder() | |
.baseUrl('https://api.github.com/') | |
.addConverterFactory(JacksonConverterFactory.create(objectMapper)) | |
.build() | |
.create(GitHub) | |
Call apiCall = api.listRepos('mnclimbingcoop') | |
Response response = apiCall.execute() | |
if (!response.success) { | |
log.error "${response.code()} Error calling API: ${response.errorBody()}" | |
return | |
} | |
List repos = response.body() | |
// ... your custom code below here... | |
log.info 'Found Repositories:' | |
repos.each{ log.info " * ${it.name} ${it.html_url}" } | |
} | |
} | |
protected interface GitHub { | |
@Headers(['Accept: application/json']) | |
@GET('/orgs/{org}/repos') | |
Call<List<Map>> listRepos(@Path('org') org) | |
//@Headers(['Accept: application/json']) | |
//@GET('/search') | |
//List search(@QueryMap Map<String, String> params) | |
//@Headers(['Accept: application/json']) | |
//@POST('/trigger/clear-faults/{key}') | |
//Map clearFaults(@Path('key') String key) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment