Search in sources :

Example 96 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class ItemService method fetchImage.

private byte[] fetchImage(String url) throws IOException {
    HttpUrl httpUrl = HttpUrl.parse(url);
    Request request = new Request.Builder().url(httpUrl).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        if (!response.isSuccessful()) {
            throw new IOException("Unsuccessful http response: " + response.message());
        }
        return response.body().bytes();
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Example 97 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class ItemService method fetchRSSearch.

public RSSearch fetchRSSearch(String query) throws IOException {
    // rs api seems to require lowercase
    query = query.toLowerCase();
    HttpUrl searchUrl = RS_SEARCH_URL.newBuilder().addQueryParameter("alpha", query).build();
    Request request = new Request.Builder().url(searchUrl).build();
    return fetchJson(request, RSSearch.class);
}
Also used : Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 98 with HttpUrl

use of okhttp3.HttpUrl in project drill by apache.

the class HttpXMLBatchReader method open.

@Override
public boolean open(SchemaNegotiator negotiator) {
    HttpUrl url = buildUrl();
    // Result set loader setup
    String tempDirPath = negotiator.drillConfig().getString(ExecConstants.DRILL_TMP_DIR);
    // Create user-friendly error context
    CustomErrorContext errorContext = new ChildErrorContext(negotiator.parentErrorContext()) {

        @Override
        public void addContext(UserException.Builder builder) {
            super.addContext(builder);
            builder.addContext("URL", url.toString());
        }
    };
    negotiator.setErrorContext(errorContext);
    // Http client setup
    SimpleHttp http = SimpleHttp.builder().scanDefn(subScan).url(url).tempDir(new File(tempDirPath)).paginator(paginator).proxyConfig(proxySettings(negotiator.drillConfig(), url)).errorContext(errorContext).build();
    // Get the input stream
    inStream = http.getInputStream();
    // Initialize the XMLReader the reader
    try {
        xmlReader = new XMLReader(inStream, dataLevel, maxRecords);
        resultLoader = negotiator.build();
        if (implicitColumnsAreProjected()) {
            implicitColumns = new ImplicitColumns(resultLoader.writer());
            buildImplicitColumns();
            populateImplicitFieldMap(http);
        }
        RowSetLoader rootRowWriter = resultLoader.writer();
        xmlReader.open(rootRowWriter, errorContext);
        xmlReader.implicitFields(implicitColumns);
    } catch (XMLStreamException e) {
        throw UserException.dataReadError(e).message("Error opening XML stream: " + e.getMessage()).addContext(errorContext).build(logger);
    }
    return true;
}
Also used : SimpleHttp(org.apache.drill.exec.store.http.util.SimpleHttp) XMLStreamException(javax.xml.stream.XMLStreamException) ChildErrorContext(org.apache.drill.common.exceptions.ChildErrorContext) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) File(java.io.File) HttpUrl(okhttp3.HttpUrl) XMLReader(org.apache.drill.exec.store.xml.XMLReader) ImplicitColumns(org.apache.drill.exec.store.ImplicitColumnUtils.ImplicitColumns) CustomErrorContext(org.apache.drill.common.exceptions.CustomErrorContext)

Example 99 with HttpUrl

use of okhttp3.HttpUrl in project drill by apache.

the class AccessTokenInterceptor method newRequestWithAccessToken.

private Request newRequestWithAccessToken(Request request, String accessToken) {
    logger.debug("Interceptor making new request with access token: {}", request.url());
    String tokenType = accessTokenRepository.getTokenType();
    if (StringUtils.isNotEmpty(tokenType)) {
        accessToken = tokenType + " " + accessToken;
    }
    if (accessTokenRepository.getOAuthConfig().isAccessTokenInHeader()) {
        HttpUrl rawUrl = HttpUrl.parse(request.url().toString());
        rawUrl.newBuilder().addQueryParameter("access_token", accessToken);
        return request.newBuilder().url(rawUrl.url()).build();
    } else {
        return request.newBuilder().header("Authorization", accessToken).build();
    }
}
Also used : HttpUrl(okhttp3.HttpUrl)

Example 100 with HttpUrl

use of okhttp3.HttpUrl in project drill by apache.

the class HttpBatchReader method open.

@Override
public boolean open(SchemaNegotiator negotiator) {
    // Result set loader setup
    String tempDirPath = negotiator.drillConfig().getString(ExecConstants.DRILL_TMP_DIR);
    HttpUrl url = buildUrl();
    logger.debug("Final URL: {}", url);
    CustomErrorContext errorContext = new ChildErrorContext(negotiator.parentErrorContext()) {

        @Override
        public void addContext(UserException.Builder builder) {
            super.addContext(builder);
            builder.addContext("URL", url.toString());
        }
    };
    negotiator.setErrorContext(errorContext);
    // Http client setup
    SimpleHttp http = SimpleHttp.builder().scanDefn(subScan).url(url).tempDir(new File(tempDirPath)).proxyConfig(proxySettings(negotiator.drillConfig(), url)).errorContext(errorContext).build();
    // JSON loader setup
    resultSetLoader = negotiator.build();
    if (implicitColumnsAreProjected()) {
        implicitColumns = new ImplicitColumns(resultSetLoader.writer());
        buildImplicitColumns();
    }
    InputStream inStream = http.getInputStream();
    populateImplicitFieldMap(http);
    try {
        JsonLoaderBuilder jsonBuilder = new JsonLoaderBuilder().implicitFields(implicitColumns).resultSetLoader(resultSetLoader).standardOptions(negotiator.queryOptions()).maxRows(maxRecords).dataPath(subScan.tableSpec().connectionConfig().dataPath()).errorContext(errorContext).fromStream(inStream);
        if (subScan.tableSpec().connectionConfig().jsonOptions() != null) {
            JsonLoaderOptions jsonOptions = subScan.tableSpec().connectionConfig().jsonOptions().getJsonOptions(negotiator.queryOptions());
            jsonBuilder.options(jsonOptions);
        } else {
            jsonBuilder.standardOptions(negotiator.queryOptions());
        }
        jsonLoader = jsonBuilder.build();
    } catch (Throwable t) {
        // Paranoia: ensure stream is closed if anything goes wrong.
        // After this, the JSON loader will close the stream.
        AutoCloseables.closeSilently(inStream);
        throw t;
    }
    return true;
}
Also used : SimpleHttp(org.apache.drill.exec.store.http.util.SimpleHttp) InputStream(java.io.InputStream) ChildErrorContext(org.apache.drill.common.exceptions.ChildErrorContext) Builder(okhttp3.HttpUrl.Builder) JsonLoaderBuilder(org.apache.drill.exec.store.easy.json.loader.JsonLoaderImpl.JsonLoaderBuilder) ProxyBuilder(org.apache.drill.exec.store.http.util.HttpProxyConfig.ProxyBuilder) JsonLoaderBuilder(org.apache.drill.exec.store.easy.json.loader.JsonLoaderImpl.JsonLoaderBuilder) JsonLoaderOptions(org.apache.drill.exec.store.easy.json.loader.JsonLoaderOptions) File(java.io.File) HttpUrl(okhttp3.HttpUrl) ImplicitColumns(org.apache.drill.exec.store.ImplicitColumnUtils.ImplicitColumns) CustomErrorContext(org.apache.drill.common.exceptions.CustomErrorContext)

Aggregations

HttpUrl (okhttp3.HttpUrl)302 Request (okhttp3.Request)130 Test (org.junit.Test)86 Response (okhttp3.Response)69 IOException (java.io.IOException)61 MockResponse (okhttp3.mockwebserver.MockResponse)40 Cookie (okhttp3.Cookie)35 ArrayList (java.util.ArrayList)28 OkHttpClient (okhttp3.OkHttpClient)27 MockWebServer (okhttp3.mockwebserver.MockWebServer)26 InputStream (java.io.InputStream)21 InputStreamReader (java.io.InputStreamReader)20 HashMap (java.util.HashMap)19 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 RequestBody (okhttp3.RequestBody)18 JsonParseException (com.google.gson.JsonParseException)13 File (java.io.File)12 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)10 List (java.util.List)10 Test (org.junit.jupiter.api.Test)10