REST service for Janet. Supports fully customizable requests, http-clients and converters.
ActionService httpService = new HttpActionService(API_URL, new OkClient(), new GsonConverter(new Gson()))
Janet janet = new Janet.Builder().addService(httpService).build();Service requires: End-point url, HttpClient and Converter.
@HttpAction(value = "/demo", method = HttpAction.Method.GET)
public class ExampleAction {
@Response ExampleDataModel responseData;
}Each action is an individual class that contains all information about the request and response.
It must be annotated with @HttpAction
ActionPipe<ExampleAction> actionPipe = janet.createPipe(ExampleAction.class);
actionPipe
.createObservable(new ExampleAction())
.subscribeOn(Schedulers.io())
.subscribe(new ActionStateSubscriber<ExampleAction>()
.onProgress((action, progress) -> System.out.println("Current progress: " + progress))
.onSuccess(action -> System.out.println("Got example " + action))
.onFail((action, throwable) -> System.err.println("Bad things happened " + throwable))
);Request path, method and type are defined by @HttpAction annotation:
value– url pathmethod– get/post/put/delete/head/patchtype– simple/multipart/form_url_encoded
To configure request, Action fields can be annotated with:
@Pathfor path value@Urlrewrites full url or suffixes@Queryfor request URL parameters@Bodyfor POST request body@Fieldfor request fields if request type isHttpAction.Type.FORM_URL_ENCODED@Partfor multipart request parts@RequestHeaderfor request headers
To process response, special annotations can be used:
@Responsefor getting response body.@Statusfor getting response status. Field typesInteger,Long,intorlongcan be used to get status code or usebooleanto know that request was sent successfully@ResponseHeaderfor getting response headers
Example:
@HttpAction(
value = "/demo/{examplePath}/info",
method = HttpAction.Method.GET,
type = HttpAction.Type.SIMPLE
)
public class ExampleAction {
// Request params
@Path("examplePath") String pathValue;
@Query("someParam") int queryParam;
@RequestHeader("Example-RequestHeader-Name") String requestHeaderValue;
// Response data
@Status int statusCode;
@Response ExampleDataModel exampleDataModel;
@ResponseHeader("Example-ResponseHeader-Name") String responseHeaderValue;
}- supports request progress;
- supports request cancelation;
- provides useful
HttpExceptionfor failed requests; - supports action inheritance
- based on annotation processing
- consider using javac option
'-Ajanet.http.factory.class.suffix=MyLib'for api libraries
Kotlin action classes are supported except internal modifier. See UsersAction as example.
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.techery.janet-http:service:xxx'
apt 'com.github.techery.janet-http:service-compiler:xxx'
compile 'com.github.techery.janet-http:client-okhttp:xxx'
compile 'com.github.techery.janet-converters:gson:yyy'
// it is recommended you also explicitly depend on latest Janet version for bug fixes and new features.
compile 'com.github.techery:janet:zzz'
}- Authorize requests via
ActionServiceWrapper, e.g. AuthWrapper - Log requests via
HttpClientorActionServiceWrapper, e.g. SampleLoggingService - Convert
Retrofitinterfaces into actions with Converter Util - Write tests using
MockHttpActionService - See more samples: Simple Android app, Advanced Android app
- Add Rules to your proguard config.
- See Android Sample for complete proguard config example.
HttpActionService is highly inspired by Retrofit2 – thanks guys, it's awesome!
We put our effort to make it even more flexible and reusable, so everyone who loves Retrofit and reactive approach should give it a try.
Copyright (c) 2016 Techery
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.