Skip to content

Instantly share code, notes, and snippets.

@davidpelayo
Last active September 22, 2015 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpelayo/16249a56ab953e165e51 to your computer and use it in GitHub Desktop.
Save davidpelayo/16249a56ab953e165e51 to your computer and use it in GitHub Desktop.
Simple Github API client using Groovy RESTClient
/**
* Simple Github API client that supports basic auth.
*/
class GithubClient {
String username
String password
String owner
String repository
String fetchFileContents(String filePath) {
request("${repoUrl}contents/${filePath}").content.decodeBase64()
}
public Map request(String url) {
githubApi.get(path : url).responseData
}
private String getRepoUrl() {
"repos/${owner}/${repository}/"
}
private RESTClient getGithubApi() {
return new RESTClient("https://api.github.com/").with {
headers.'User-Agent' = 'Mozilla/5.0'
if (username && password) {
headers['Authorization'] = 'Basic '+"${username}:${password}".getBytes('iso-8859-1').encodeBase64()
}
it
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment