diff --git a/.gitignore b/.gitignore index 4ba2e23..00f3089 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # target folders */target/ +/target # Mac .DS_Store diff --git a/README.md b/README.md index 382ec5d..aeebce2 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ **FoxHttp provides a fast and easy http client for Java and Android. It's part of the GroundWork Project by Viascom.** -[![master](https://img.shields.io/badge/master-v1.2.1-brightgreen.svg)](https://github.com/Viascom/FoxHttp/tree/master) -[![develop](https://img.shields.io/badge/develop-v1.3--RC1-brightgreen.svg)](https://github.com/Viascom/FoxHttp/tree/develop) +[![master](https://img.shields.io/badge/master-v1.3--RC1-brightgreen.svg)](https://github.com/Viascom/FoxHttp/tree/master) +[![develop](https://img.shields.io/badge/develop-v1.3--RC2-brightgreen.svg)](https://github.com/Viascom/FoxHttp/tree/develop) [![Maven Central](https://img.shields.io/maven-central/v/ch.viascom.groundwork/foxhttp.svg)]() [![Bintray](https://img.shields.io/bintray/v/viascom/GroundWork/ch.viascom.groundwork%3Afoxhttp.svg)]()
[![Size](https://img.shields.io/badge/size-197.2_KB-brightgreen.svg)]() @@ -39,3 +39,62 @@ Request against [httpbin](https://httpbin.org/) which was installed on localhost * URL placeholder support * _Advanced cache strategy (coming soon)_ * _GroundWork Server-Security support (coming soon)_ +* _HAL support (coming soon)_ + +## Quick Start: + +### Dependency + +#### maven +```xml + + ch.viascom.groundwork + foxhttp + 1.3-RC2 + +``` + +#### gradle +``` +compile 'ch.viascom.groundwork:foxhttp:1.3-RC2' +``` + +### Send a request with JSON response deserialization +To run this example you need to add Gson to your dependency management! +```java +// Define Http-Client and set parser for serialization/deserialization +FoxHttpClient foxHttpClient = new FoxHttpClientBuilder(new GsonParser()).build(); + +// Define a System-Out logger +foxHttpClient.setFoxHttpLogger(new SystemOutFoxHttpLogger(true, "FoxHttp-Logger")); + +// Create and Execute GET Request +FoxHttpResponse response = new FoxHttpRequestBuilder("http://httpbin.org/get?search=Viascom", RequestType.GET, foxHttpClient).executeNow(); + +// Deserialization response +GetResponse object = repsponse.getParsedBody(GetResponse.class); + +// Print result +System.out.println("Parsed-Output: " + object); +``` + +To deserialize the response you need the following model: +```java +public class GetResponse implements Serializable { + + public HashMap args; + public HashMap headers; + public String origin; + public String url; + + @Override + public String toString() { + return "GetResponse{" + + "args=" + args + + ", headers=" + headers + + ", origin='" + origin + '\'' + + ", url='" + url + '\'' + + '}'; + } +} +``` \ No newline at end of file diff --git a/pom.xml b/pom.xml index 98212ed..7401109 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ foxhttp - 1.3-RC1 + 1.3-RC2 GroundWork - FoxHttp diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SkipResponseBody.java b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SkipResponseBody.java index bb9c858..67c4455 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SkipResponseBody.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SkipResponseBody.java @@ -3,7 +3,7 @@ import java.lang.annotation.*; /** - * The @SkipResponseBody annotation will skip the response body if set to true. The default is false. + * The @SkipResponseBody annotation will skip the response body if set to true. If the annotation is present, the default is true. * * @author patrick.boesch@viascom.ch */ @@ -11,5 +11,5 @@ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface SkipResponseBody { - boolean value(); + boolean value() default true; } diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpClientBuilder.java b/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpClientBuilder.java index dde43e3..2a59f09 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpClientBuilder.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpClientBuilder.java @@ -12,6 +12,8 @@ import ch.viascom.groundwork.foxhttp.interceptor.response.GZipResponseInterceptor; import ch.viascom.groundwork.foxhttp.log.FoxHttpLogger; import ch.viascom.groundwork.foxhttp.parser.FoxHttpParser; +import ch.viascom.groundwork.foxhttp.parser.GsonParser; +import ch.viascom.groundwork.foxhttp.parser.XStreamParser; import ch.viascom.groundwork.foxhttp.placeholder.FoxHttpPlaceholderStrategy; import ch.viascom.groundwork.foxhttp.proxy.FoxHttpProxyStrategy; import ch.viascom.groundwork.foxhttp.ssl.FoxHttpHostTrustStrategy; @@ -112,6 +114,39 @@ public FoxHttpClientBuilder setFoxHttpRequestParser(FoxHttpParser foxHttpRequest return this; } + /** + * Set the request and response parser + * + * @param foxHttpParser a FoxHttpParser + * + * @return FoxHttpClientBuilder (this) + */ + public FoxHttpClientBuilder setFoxHttpParser(FoxHttpParser foxHttpParser){ + setFoxHttpRequestParser(foxHttpParser); + setFoxHttpResponseParser(foxHttpParser); + return this; + } + + /** + * Activate default gson parser for json + * + * @return FoxHttpClientBuilder (this) + */ + public FoxHttpClientBuilder activateGsonParser(){ + setFoxHttpParser(new GsonParser()); + return this; + } + + /** + * Activate default xstream parser for xml + * + * @return FoxHttpClientBuilder (this) + */ + public FoxHttpClientBuilder activateXStreamParser(){ + setFoxHttpParser(new XStreamParser()); + return this; + } + /** * Define a map of FoxHttpInterceptors * This will override the existing map of interceptors diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpRequestBuilder.java b/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpRequestBuilder.java index 71864db..de5e5cb 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpRequestBuilder.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/builder/FoxHttpRequestBuilder.java @@ -2,6 +2,7 @@ import ch.viascom.groundwork.foxhttp.FoxHttpClient; import ch.viascom.groundwork.foxhttp.FoxHttpRequest; +import ch.viascom.groundwork.foxhttp.FoxHttpResponse; import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorization; import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope; import ch.viascom.groundwork.foxhttp.body.request.FoxHttpRequestBody; @@ -384,4 +385,13 @@ public FoxHttpRequest build() throws FoxHttpRequestException { return request; } + /** + * Build and execute the FoxHttpRequest of this builder + * + * @return FoxHttpResponse response of the request + */ + public FoxHttpResponse executeNow() throws FoxHttpException { + return build().execute(); + } + } diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/type/ContentType.java b/src/main/java/ch/viascom/groundwork/foxhttp/type/ContentType.java index 5245d1f..5031708 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/type/ContentType.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/type/ContentType.java @@ -16,6 +16,7 @@ public class ContentType { public static final ContentType APPLICATION_ATOM_XML = create("application/atom+xml", Charset.forName(UTF8)); public static final ContentType APPLICATION_FORM_URLENCODED = create("application/x-www-form-urlencoded", Charset.forName(UTF8)); public static final ContentType APPLICATION_JSON = create("application/json", Charset.forName(UTF8)); + public static final ContentType APPLICATION_HAL_JSON = create("application/hal+json", Charset.forName(UTF8)); public static final ContentType APPLICATION_OCTET_STREAM = create("application/octet-stream", (Charset) null); public static final ContentType APPLICATION_SVG_XML = create("application/svg+xml", Charset.forName(UTF8)); public static final ContentType APPLICATION_XHTML_XML = create("application/xhtml+xml", Charset.forName(UTF8));