Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't use HttpHelper to make requests for Travis API #71

Open
jvcoutinho opened this issue Nov 20, 2019 · 0 comments
Open

I can't use HttpHelper to make requests for Travis API #71

jvcoutinho opened this issue Nov 20, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@jvcoutinho
Copy link
Collaborator

Problem
Currently, I can't use HttpHelper class for TravisAPI requests, having to implement my own requestor.

static String getBuildAttribute(Project project, String attribute, String branchName) {
String url = "https://api.travis-ci.com/repo/${projectOwnerName}%2F${project.getName()}/branch/${branchName}"
HttpURLConnection connection = new URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty("Travis-API-Version", "3")
connection.setRequestProperty("Authorization", "token ${travisAPIToken}")
if (connection.getResponseCode() != 200) {
Thread.sleep(3000) // sleep for some seconds and try again
return getBuildAttribute(project, attribute, branchName)
}
def last_build = HttpHelper.responseToJSON(connection.getInputStream())['last_build']
if (last_build == null) {
Thread.sleep(3000) // sleep for some seconds and try again
return getBuildAttribute(project, attribute, branchName)
}
return last_build[attribute]
}

Cause
The requisition needs the field "Travis-API-Version" in the header and HttpHelper doesn't allow me to include it.

public static HttpURLConnection requestToApi(String url, String method, String token) {
try {
def request = new URL(url).openConnection();
if (token.length() > 0) {
request.setRequestProperty("Authorization", getAuthorizationHeader(token))
}
request.setRequestMethod(method)
return request
} catch (IOException e) {
throw new HttpException("Error sending the HTTP request")
} catch (UnknownHostException e) {
throw new HttpException("Unable to find request Host")
}
}
public static HttpURLConnection requestToApi(String url, String method) {
try {
def request = new URL(url).openConnection();
request.setRequestMethod(method)
return request
} catch (IOException e) {
throw new HttpException("Error sending the HTTP request")
} catch (UnknownHostException e) {
throw new HttpException("Unable to find request Host")
}
}

Suggestion
Include an additional map parameter to requestToApi methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant