Search in sources :

Example 61 with HttpHeaders

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"));
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) Test(org.junit.Test)

Example 62 with HttpHeaders

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);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl)

Example 63 with HttpHeaders

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");
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) StringWriter(java.io.StringWriter) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException)

Aggregations

HttpHeaders (com.google.api.client.http.HttpHeaders)63 HttpRequest (com.google.api.client.http.HttpRequest)32 IOException (java.io.IOException)26 GenericUrl (com.google.api.client.http.GenericUrl)22 HttpResponse (com.google.api.client.http.HttpResponse)21 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)15 Test (org.junit.Test)8 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)7 ByteArrayContent (com.google.api.client.http.ByteArrayContent)6 HttpContent (com.google.api.client.http.HttpContent)6 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)6 HttpResponseException (com.google.api.client.http.HttpResponseException)6 JsonObjectParser (com.google.api.client.json.JsonObjectParser)5 Map (java.util.Map)5 HttpTransport (com.google.api.client.http.HttpTransport)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 Objects (com.google.api.services.storage.model.Objects)4 SocketTimeoutException (java.net.SocketTimeoutException)4 URI (java.net.URI)4