Last active
September 22, 2015 08:52
-
-
Save davidpelayo/16249a56ab953e165e51 to your computer and use it in GitHub Desktop.
Simple Github API client using Groovy RESTClient
This file contains hidden or 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
/** | |
* 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