All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenticocloud.delivery_rx.JavaHttpAdapter Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright 2018 Kentico s.r.o. and Richard Sustek
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.kenticocloud.delivery_rx;


import com.kenticocloud.delivery_core.adapters.IHttpAdapter;
import com.kenticocloud.delivery_core.interfaces.item.common.IQueryConfig;
import com.kenticocloud.delivery_core.models.common.Header;
import com.kenticocloud.delivery_core.models.exceptions.KenticoCloudException;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

class JavaHttpAdapter implements IHttpAdapter {

    private static OkHttpClient okHttpClient = new OkHttpClient();

    @Override
    public String get(String url, IQueryConfig queryConfig, List
headers) { Request.Builder builder = new Request.Builder() .url(url); for(Header header : headers){ builder.addHeader(header.getName(), header.getValue()); } Request request = builder.build(); Response response; try { response = okHttpClient.newCall(request).execute(); ResponseBody body = response.body(); if (body == null){ throw new NullPointerException("Response from server is null"); } return body.string(); } catch (IOException e) { e.printStackTrace(); throw new KenticoCloudException(e.getMessage(), e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy