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

io.continual.jsonHttpClient.impl.ok.OkRequest Maven / Gradle / Ivy

There is a newer version: 0.3.16
Show newest version
package io.continual.jsonHttpClient.impl.ok;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.continual.jsonHttpClient.HttpUsernamePasswordCredentials;
import io.continual.jsonHttpClient.JsonOverHttpClient.HttpRequest;
import io.continual.jsonHttpClient.JsonOverHttpClient.HttpResponse;
import io.continual.jsonHttpClient.JsonOverHttpClient.HttpServiceException;
import io.continual.util.data.TypeConvertor;
import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

class OkRequest implements HttpRequest
{
	public OkRequest ( OkHttpClient httpClient )
	{
		fHttpClient = httpClient;
		fHeaders = new HashMap<>();
	}

	@Override
	public HttpRequest onPath ( String url )
	{
		fPath = url;
		return this;
	}

	@Override
	public HttpRequest asUser ( HttpUsernamePasswordCredentials creds )
	{
		fCreds = creds;
		return this;
	}

	@Override
	public HttpRequest withHeader ( String name, String value )
	{
		List set = fHeaders.get ( name );
		if ( set == null )
		{
			set = new LinkedList();
			fHeaders.put ( name, set );
		}
		set.add ( value );
		
		return this;
	}

	@Override
	public HttpRequest withHeaders ( Map headers )
	{
		for ( Map.Entry e : headers.entrySet () )
		{
			withHeader ( e.getKey (), e.getValue () );
		}
		return this;
	}

	@Override
	public HttpRequest withExplicitQueryString ( String qs )
	{
		fQueryString = qs;
		fQueryParams = null;
		return this;
	}

	@Override
	public HttpRequest addQueryParam ( String key, String val )
	{
		fQueryString = null;
		if ( fQueryParams == null )
		{
			fQueryParams = new HashMap<> ();
		}
		fQueryParams.put ( key, val );
		return this;
	}

	@Override
	public HttpRequest withQueryString ( Map qsMap )
	{
		for ( Map.Entry e : qsMap.entrySet () )
		{
			addQueryParam ( e.getKey (), e.getValue () );
		}
		return this;
	}

	private Request.Builder basicReq ()
	{
		String path = fPath;

		if ( fQueryString != null && fQueryString.length () > 0 )
		{
			path = path + "?" + fQueryString;
		}
		else if ( fQueryParams != null && fQueryParams.size () > 0 )
		{
			final StringBuilder sb = new StringBuilder ();
			for ( Map.Entry e : fQueryParams.entrySet () )
			{
				if ( sb.length () > 0 )
				{
					sb.append ( "&" );
				}
				sb
					.append ( e.getKey () )
					.append ( "=" )
					.append ( TypeConvertor.urlEncode ( e.getValue () )	)
				;
			}
			path = path + "?" + sb.toString ();
		}

		Request.Builder rb = new Request.Builder ()
			.url ( path )
		;

		if ( fCreds != null )
		{
			rb = rb.header ( "Authorization", Credentials.basic ( fCreds.getUser (), fCreds.getPassword () ) );
		}

		for ( Map.Entry> e : fHeaders.entrySet () )
		{
			for ( String val : e.getValue () )
			{
				rb.addHeader ( e.getKey (), val );
			}
		}

		return rb;
	}

	@Override
	public HttpResponse get () throws HttpServiceException
	{
		return run ( "GET", basicReq ().build () );
	}

	@Override
	public HttpResponse delete () throws HttpServiceException
	{
		return run ( "DEL", basicReq ().delete().build () );
	}

	@Override
	public HttpResponse put ( JSONObject body ) throws HttpServiceException
	{
		final RequestBody rb = RequestBody.create (
			MediaType.parse("application/json"),
			body.toString ()
		);
		return run ( "PUT", basicReq ().put(rb).build () );
	}

	@Override
	public HttpResponse patch ( JSONObject body ) throws HttpServiceException
	{
		final RequestBody rb = RequestBody.create (
			MediaType.parse("application/json"),
			body.toString ()
		);
		return run ( "PAT", basicReq ().patch( rb ).build () );
	}

	@Override
	public HttpResponse post ( JSONObject body ) throws HttpServiceException
	{
		final RequestBody rb = RequestBody.create (
			MediaType.parse("application/json"),
			body.toString ()
		);
		return run ( "POS", basicReq ().post(rb).build () );
	}

	@Override
	public HttpResponse post ( JSONArray body ) throws HttpServiceException
	{
		final RequestBody rb = RequestBody.create (
			MediaType.parse("application/json"),
			body.toString ()
		);
		return run ( "POS", basicReq ().post(rb).build () );
	}

	private final OkHttpClient fHttpClient;

	private String fPath;
	private HttpUsernamePasswordCredentials fCreds;
	private HashMap> fHeaders;
	private String fQueryString = null;
	private HashMap fQueryParams = null;

	private HttpResponse run ( String verbForLog, Request request ) throws HttpServiceException
	{
		try
		{
			final String user = fCreds == null ? " (anon)" : " (as " + fCreds.getUser () + ")";
			log.info ( "HTTP {} " + fPath + user, verbForLog );

			final long startMs = System.nanoTime ();
			final Response response = fHttpClient.newCall ( request ).execute ();
			final long endMs = System.nanoTime ();

			final int code = response.code ();
			log.info ( "    -> {}; {} ms", code, ( endMs - startMs ) / 1000L / 1000L );
			
			return new OkResponse ( code, response );
		}
		catch ( IOException | JSONException e )
		{
			throw new HttpServiceException ( e );
		}
	}

	private static final Logger log = LoggerFactory.getLogger ( OkRequest.class );
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy