Skip to content

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

Open

Description

@jvcoutinho

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions