use of com.google.api.client.http.HttpHeaders in project googleads-java-lib by googleads.
the class ReportRequestFactoryHelperTest method testGetHttpRequestFactory.
/**
* Tests the factory builds the request properly for this test's attributes.
*/
@Test
public void testGetHttpRequestFactory() throws ValidationException, AuthenticationException, IOException {
final int timeoutFromLibConfig = 42;
when(adWordsLibConfiguration.getReportDownloadTimeout()).thenReturn(timeoutFromLibConfig);
AdWordsSession session = new AdWordsSession.Builder().withDeveloperToken("foodevtoken").withClientCustomerId("fooclientcustomerid").withOAuth2Credential(credential).withUserAgent("userAgent").withReportingConfiguration(reportingConfiguration).build();
when(authorizationHeaderProvider.getAuthorizationHeader(session, ENDPOINT_URL.build())).thenReturn("fooauthheader");
when(userAgentCombiner.getUserAgent(anyString())).thenReturn("foouseragent");
ReportRequestFactoryHelper helper = new ReportRequestFactoryHelper(session, authorizationHeaderProvider, userAgentCombiner, transport, adWordsLibConfiguration, reportResponseInterceptor);
HttpRequestFactory requestFactory = helper.getHttpRequestFactory(ENDPOINT_URL.build(), version);
HttpRequest request = requestFactory.buildPostRequest(ENDPOINT_URL, new AwqlReportBodyProvider("select 1", "csv").getHttpContent());
HttpHeaders headers = request.getHeaders();
assertEquals("foodevtoken", headers.get("developerToken"));
assertEquals("fooauthheader", headers.getAuthorization());
assertEquals("fooclientcustomerid", headers.get("clientCustomerId"));
assertTrue((headers.getUserAgent()).contains("foouseragent"));
if (reportingConfiguration == null) {
assertFalse("skipReportHeader should not be in the header if no reporting config is set", headers.containsKey("skipReportHeader"));
assertFalse("skipReportSummary should not be in the header if no reporting config is set", headers.containsKey("skipReportSummary"));
assertEquals("connect timeout is incorrect", timeoutFromLibConfig, request.getConnectTimeout());
assertEquals("read timeout is incorrect", timeoutFromLibConfig, request.getReadTimeout());
} else {
Integer expectedTimeout = reportingConfiguration.getReportDownloadTimeout();
if (expectedTimeout == null) {
// Should fall back to the library level config value if the reporting config does not have
// a timeout set.
expectedTimeout = timeoutFromLibConfig;
}
assertEquals("connect timeout is incorrect", expectedTimeout.intValue(), request.getConnectTimeout());
assertEquals("read timeout is incorrect", expectedTimeout.intValue(), request.getReadTimeout());
assertEquals("skipReportHeader not equal to the reporting config setting", toStringBoolean(reportingConfiguration.isSkipReportHeader()), headers.get("skipReportHeader"));
assertEquals("skipColumnHeader not equal to the reporting config setting", toStringBoolean(reportingConfiguration.isSkipColumnHeader()), headers.get("skipColumnHeader"));
assertEquals("skipReportSummary not equal to the reporting config setting", toStringBoolean(reportingConfiguration.isSkipReportSummary()), headers.get("skipReportSummary"));
assertEquals("includeZeroImpressions not equal to the reporting config setting", toStringBoolean(reportingConfiguration.isIncludeZeroImpressions()), headers.get("includeZeroImpressions"));
assertEquals("useRawEnumValues not equal to the reporting config setting", toStringBoolean(reportingConfiguration.isUseRawEnumValues()), headers.get("useRawEnumValues"));
}
}
use of com.google.api.client.http.HttpHeaders in project data-transfer-project by google.
the class MastodonHttpUtilities method postStatus.
/**
* Posts a new status for the user, initially marked as private.*
*/
public void postStatus(String content, String idempotencyKey) throws IOException {
ImmutableMap<String, String> formParams = ImmutableMap.of("status", content, // Default everything to private to avoid a privacy incident
"visibility", "private");
UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formParams);
HttpRequest postRequest = TRANSPORT.createRequestFactory().buildPostRequest(new GenericUrl(baseUrl + POST_URL), urlEncodedContent).setThrowExceptionOnExecuteError(false);
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("Bearer " + accessToken);
if (!Strings.isNullOrEmpty(idempotencyKey)) {
// This prevents the same post from being posted twice in the case of network errors
headers.set("Idempotency-Key", idempotencyKey);
}
postRequest.setHeaders(headers);
HttpResponse response = postRequest.execute();
validateResponse(postRequest, response, 200);
}
use of com.google.api.client.http.HttpHeaders in project data-transfer-project by google.
the class SolidUtilities method getModel.
/**
* Parses the contents of a URL to produce an RDF model.
*/
public Model getModel(String url) throws IOException {
HttpRequestFactory factory = TRANSPORT.createRequestFactory();
HttpRequest rootGetRequest = factory.buildGetRequest(new GenericUrl(url));
HttpHeaders headers = new HttpHeaders();
headers.setCookie(authCookie);
headers.setAccept("text/turtle");
rootGetRequest.setHeaders(headers);
HttpResponse response = rootGetRequest.execute();
if (response.getStatusCode() != 200) {
throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage());
}
StringWriter writer = new StringWriter();
IOUtils.copy(response.getContent(), writer, "UTF-8");
String fixedString = fixProblematicPeriods(writer.toString());
Model defaultModel = ModelFactory.createDefaultModel();
return defaultModel.read(new StringReader(fixedString), url, "TURTLE");
}
Aggregations