All Projects → FabianTerhorst → Apiclient

FabianTerhorst / Apiclient

Licence: apache-2.0
A easy to use api client that combines the power of Retrofit, Realm, Gson, Rxjava and Retrolambda in a easy to use library for Java and Android

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Apiclient

RxRealm
Utilities for using RxJava with Realm
Stars: ✭ 23 (-77%)
Mutual labels:  rxjava, realm
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (+0%)
Mutual labels:  rxjava, gson
T Mvp
Android AOP Architecture by Apt, AspectJ, Javassisit, based on Realm+Databinding+MVP+Retrofit+Rxjava2
Stars: ✭ 2,740 (+2640%)
Mutual labels:  rxjava, realm
NewsReader
Android News Reader app. Kotlin Coroutines, Retrofit and Realm
Stars: ✭ 21 (-79%)
Mutual labels:  rxjava, realm
Easygank
💊 The project build framework based on the Rx series and MVP pattern.
Stars: ✭ 750 (+650%)
Mutual labels:  rxjava, gson
UseCases
This a library that offers a generic implementation of the data layers from the clean architecture by Uncle bob.
Stars: ✭ 23 (-77%)
Mutual labels:  rxjava, realm
Geeknews
📚A pure reading App based on Material Design + MVP + RxJava2 + Retrofit + Dagger2 + Realm + Glide
Stars: ✭ 3,496 (+3396%)
Mutual labels:  rxjava, realm
Kotlin Cleanarchitecture
This is a sample app that is part of a blog post I have written about how to architect android application using the Uncle Bob's clean architecture and Fernando Cejas Android-CleanArchitecture in Kotlin. Post in Spanish: http://katade.com/clean-architecture-kotlin/
Stars: ✭ 274 (+174%)
Mutual labels:  rxjava, realm
Reservoir
Android library to easily serialize and cache your objects to disk using key/value pairs.
Stars: ✭ 674 (+574%)
Mutual labels:  rxjava, gson
Countries
An example Android app using Retrofit, Realm, Parceler, Dagger and the MVVM pattern with the data binding lib.
Stars: ✭ 616 (+516%)
Mutual labels:  gson, realm
SilverScreener
A feature-rich movie guide app, that lets you discover movies from TMDb.
Stars: ✭ 24 (-76%)
Mutual labels:  gson, realm
Android Mvp Starter
Create/Generate your MVP projects easily
Stars: ✭ 72 (-28%)
Mutual labels:  rxjava, realm
Awesome Blogs Android
어썸블로그 ・ 개발 블로그 모음 ・ 개발 잡덕들을 위한 본격 고퀄리티 개발 블로그 큐레이션 서비스 🕵️‍♀️
Stars: ✭ 128 (+28%)
Mutual labels:  rxjava, realm
nakadi-java
🌀 Client library for the Nakadi Event Broker (examples: http://bit.ly/njc-examples, site: https://dehora.github.io/nakadi-java/)
Stars: ✭ 29 (-71%)
Mutual labels:  rxjava, gson
Androidrapidlibrary
Android 快速开发库,主要想实现一条属于自己的开发框架。包括网络访问,数据,UI等等
Stars: ✭ 577 (+477%)
Mutual labels:  rxjava, realm
Habitica Android
Native Android app for Habitica
Stars: ✭ 847 (+747%)
Mutual labels:  rxjava, realm
Newspaper
An aggregated newspaper app containing news from 10+ local news publishers in Hong Kong. Made with ❤
Stars: ✭ 82 (-18%)
Mutual labels:  rxjava, realm
Kson
Gson TypeAdapter & Factory generator for Kotlin data classes
Stars: ✭ 90 (-10%)
Mutual labels:  gson
Bgfmdb
BGFMDB让数据的增删改查分别只需要一行代码即可,就是这么简单任性,本库几乎支持存储ios所有基本的自带数据类型.
Stars: ✭ 1,344 (+1244%)
Mutual labels:  realm
Rxjava Essentials Cn
RxJava Essentials 中文翻译版 仅供交流学习使用,严禁商业用途
Stars: ✭ 1,282 (+1182%)
Mutual labels:  rxjava

ApiClient

A easy to use api client that combines the power of Retrofit, Realm, Gson, Rxjava and Retrolambda in a library for Java and Android

Add to build.gradle
compile 'io.fabianterhorst:apiclient:0.4'
compile 'io.fabianterhorst:apiclient-accountmanager:0.1'
compile 'io.fabianterhorst:apiclient-components:0.1'

First Step

Create your Api Class

public class Twitter extends ApiClient<TwitterApi> implements TwitterApi {

    public Twitter(Realm realm, String apiKey) {
        super(realm, TwitterApi.PARAM_API_KEY, apiKey, TwitterApi.class, TwitterApi.END_POINT);
    }

    public static void init(String apiKey) {
        init(new Twitter(apiKey));
    }

    @Override
    public Observable<List<Character>> getTweets() {
    	//Here you can define the tablename for realm and the fieldname if needed to sort the tweets with
        return getApiObservable(getApi().getTweets(), Tweet.class, "name");
    }

    @Override
    public Observable<List<Character>> getComments(ArrayList<Integer> ids) {
    	//You can also get results only for specific ids
        return getApiObservable(getApi().getComments(), Comment.class, "id", ids);
    }
}

Second Step

Create your Api Interface (The Retrofit way)

public interface TwitterApi {
	
	@GET("tweets")
	Observable<List<Tweet>> getTweets();

	@GET("comments")
	Observable<List<Tweet>> getComments(@Query("id") ArrayList<Integer> ids);
}

Third Step

Initiate the Singleton in the Application onCreate

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration config = new RealmConfiguration.Builder(this)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);
        Twitter.init("0123456789");
    }
}

Fourth Step

Use it and have fun. The library is handling the saving, the loading and the refreshing for you.

Twitter twitter = Twitter.getInstance();

twitter.getTweets().subscribe(tweets-> System.out.println(tweets));

FAQ

How to handle Android Activity lifecycle

You can use the ApiClient component module to get access to RxActivity and RxFragment

In your Activity you have to get the Singleton with the Activity lifecycle. Your activity has to extend RxActivity.

Twitter twitter = Twitter.getInstance(bindToLifecycle());

And thats everythink you have to do to prevent memory leaks.

RealmList doesn´t support null objects. How can i ignore null object inside the response json?

You can override the gson builder inside your api class and add custom deserializer adapters to avoid adding null objects.

@Override
public GsonBuilder getGsonBuilder(GsonBuilder gsonBuilder) {
    GsonUtils.registerRemoveNullListSerializer(gsonBuilder, new TypeToken<RealmList<MyFirstObject>>() {}, MyFirstObject.class);
    GsonUtils.registerRemoveNullListSerializer(gsonBuilder, new TypeToken<RealmList<MySecondObject>>() {}, MySecondObject.class);
    GsonUtils.registerRemoveNullListSerializer(gsonBuilder, new TypeToken<RealmList<MyThirdObject>>() {}, MyThirdObject.class);
    return gsonBuilder;
}
How to change the api key from everywhere?

You can use the setApiKey method.

Twitter.getInstance().setApiKey("9876543210");
How to add other query parameters?

You can override the getHttpUrlBuilder(HttpUrl.Builder builder) method from the api client.

@Override
public HttpUrl.Builder getHttpUrlBuilder(HttpUrl.Builder builder) {
    return addQueryParameter("lang", Locale.getDefault().getLanguage());
}
How to use a authentication

The easiest way is to use the AuthUtils to add a authentication via the request builder for post parameters and headers or the http url builder for query parameter

myurl.com/api

@Override
public Request.Builder getRequestBuilder(Request.Builder builder) {
    return AuthUtils.addDefaultAuthentication(builder, getApiKey());
}

myurl.com/api?apiKey=012345

@Override
public HttpUrl.Builder getHttpUrlBuilder(HttpUrl.Builder builder) {
    AuthUtils.addDefaultAuthentication(builder, "apiKey", getApiKey());
        return builder.addQueryParameter("lang", Locale.getDefault().getLanguage());
}

License

Copyright 2016 Fabian Terhorst

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].