Skip to content

Instantly share code, notes, and snippets.

@fijiaaron
Last active August 22, 2019 21:32
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 fijiaaron/48596eb34c7f3ee46c29bf6132d2a2b8 to your computer and use it in GitHub Desktop.
Save fijiaaron/48596eb34c7f3ee46c29bf6132d2a2b8 to your computer and use it in GitHub Desktop.
package com.saucelabs.api.examples.analytics;
import io.mikael.urlbuilder.UrlBuilder;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import static java.time.format.DateTimeFormatter.*;
import static java.time.temporal.ChronoUnit.DAYS;
public class WithHttpClient
{
String baseUrl = "https://saucelabs.com";
String TrendsEndpoint = "/rest/v1/analytics/trends/tests";
String ErrorsEndpoint = "/rest/v1/analytics/trends/errors";
String BuildsAndTestsEndpoint = "/rest/v1/analytics/trends/builds_tests";
String TestsEndpoint = "/rest/v1/analytics/tests";
String TestMetricsEndpoint = "/rest/v1/analytics/insights/test-metrics";
String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
String credentials = Base64.getEncoder().encodeToString((SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY).getBytes());
DateTimeFormatter timestamp = ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
LocalDateTime now = LocalDateTime.now();
String start = now.minus(29, DAYS).format(timestamp); // YYYY-MM-DDTHH:MM:SSZ or unix timestamp
String end = now.format(timestamp); // YYYY-MM-DDTHH:MM:SSZ or unix timestamp
String time_range = "-7d"; // alternative to start/end dates: -Nd days, h hours, m minutes, s seconds
String scope = "me"; // optional: me, organization, single
String owner = SAUCE_USERNAME; // only required if scope is "single"
String status = "errored"; // optional: passed, failed, complete, errored
String os = "Windows 10"; // optional
String browser = "Chrome"; // optional
UrlBuilder urlBuilder = UrlBuilder.fromString(baseUrl)
.addParameter("start", start)
.addParameter("end", end)
// .addParameter("time_range", time_range)
// .addParameter("scope", scope)
// .addParameter("owner", owner)
// .addParameter("status", status)
.addParameter("pretty", null);
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.setHeader("Authorization", "Basic " + credentials)
.setHeader("Accept", "application/json");
@Test
public void getTestTrends() throws IOException, InterruptedException
{
URI testTrendsURI_withParams = urlBuilder.withPath(TrendsEndpoint)
.addParameter("interval", "1d")
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(testTrendsURI_withParams);
}
@Test
public void getTestErrors() throws IOException, InterruptedException
{
URI testErrorsURI_withParams = urlBuilder.withPath(ErrorsEndpoint)
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(testErrorsURI_withParams);
}
@Test
public void getBuildsAndTests() throws IOException, InterruptedException
{
URI buildsAndTestsURI_withParams = urlBuilder.withPath(BuildsAndTestsEndpoint)
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(buildsAndTestsURI_withParams);
}
@Test
public void getBuildsAndTests_WithErrors() throws IOException, InterruptedException
{
URI buildsAndTestsURI_withParams = urlBuilder.withPath(BuildsAndTestsEndpoint)
.addParameter("status", "errored")
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(buildsAndTestsURI_withParams);
}
@Test
public void getTests() throws IOException, InterruptedException
{
String size = "100"; // number of tests returned
String from = "0"; // pagination for first test, optional
String query = "test_name"; // filter by test name, optional
String build = "build_name"; // filter by build name, optional
String missing_build = null; // return only tests with a missing build, optional
String desc = null; // sort descending (default is false), optional
URI testsURI_withParams = urlBuilder.withPath(TestsEndpoint)
.addParameter("size", size)
// .addParameter("from", from)
// .addParameter("query", query)
// .addParameter("build", build)
// .addParameter("missing_build", null)
// .addParameter("desc", null)
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(testsURI_withParams);
}
@Test
public void getTests_withErrors() throws IOException, InterruptedException
{
String size = "100"; // number of tests returned
String from = "0"; // pagination for first test, optional
String query = "test_name"; // filter by test name, optional
String build = "build_name"; // filter by build name, optional
String missing_build = null; // return only tests with a missing build
String desc = null; // sort descending (default is false), optional
URI testsURI_withParams = urlBuilder.withPath(TestsEndpoint)
.addParameter("status", "errored")
.addParameter("size", size)
// .addParameter("from", from)
// .addParameter("query", query)
// .addParameter("build", build)
// .addParameter("missing_build", null)
// .addParameter("desc", null)
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(testsURI_withParams);
}
@Test
public void getTestMetrics() throws IOException, InterruptedException
{
String query = "test_name"; // filter by test name, required
URI testMetricsURI_withParams = urlBuilder.withPath(TestMetricsEndpoint)
.addParameter("query", query)
// .addParameter("os", os)
// .addParameter("browser", browser)
.toUri();
sendRequest(testMetricsURI_withParams);
}
private void sendRequest(URI uri) throws IOException, InterruptedException
{
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = requestBuilder.GET().uri(uri).build();
System.out.println("request uri: " + uri);
System.out.println("request headers: " + request.headers());
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("response status: " + response.statusCode());
System.out.println("response body: " + response.body());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment