Coder Social home page Coder Social logo

morristech / easy.api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chiragprajapati-indianic/easy.api

0.0 1.0 0.0 1.79 MB

Easy.Api is a bundle of utility classes which helps to call the RESTful webservices as quickly as never before. It also allows use of RxJava with only fewer implementation detail. It also works with kotlin.

Java 53.90% Kotlin 46.10%

easy.api's Introduction

logo

Easy.Api

Java Java Kotlin Download

Version 2.0 is here ๐ŸŽ‰๐ŸŒŸ๐ŸคŸ

What's new:

  1. Added gradle library dependency instead of standalone classfiles
  2. Dedicated dependency for koltin projects
  3. More simplified way of usage
  4. More is coming, stay tuned

Easy.Api is a bundle of utility classes which helps to call the RESTful webservices as quickly as never before. It also allows use of RxJava with only fewer implementation detail. It also works with kotlin.

Reference Blog: https://android.jlelse.eu/easy-api-forget-boilerplates-implement-web-services-in-a-snap-5995a32f1637

It will make your life easy by...

I. Handling Network connectivity check

II. Handling Loading behavior callbacks

III. Handling Error/Exception callback

IV. Return Success/failure checks

V. Easily use with ViewModel & LiveData ๐Ÿ˜


โœ๏ธ It is just a wrapper of utility classes who uses retrofit networking library under the hood, which provides you ability to write concise and less lines of code with flexibility of changing loading and error handling behavior as per your need on specific screens of your application

Let's see it in action ๐Ÿ’ป๐Ÿ“ฒ

Integartion:

//1. Java:

   implementation 'com.hlab.easyapi:easyapi-java:2.0.0'

//2. For kotlin lovers:

   implementation 'com.hlab.easyapi:easyapi-kotlin:2.0.0'

As kotlin is interoperable with java, any of the dependency will work fine with your code written in either language

Implementation:

You are supposed to implement LoaderInterface for default implementation or you need to add LoadingLiveData in viewmodel to observe loading behavior You may add this implementation in base class to use it easily. BaseFragment.java

  1. Default request:
       EasyApiCall<Envelop<List<User>>> usersListApi 
           = new EasyApi.Builder<Envelop<List<User>>(Objects.requireNonNull(getActivity()))
             .setLoaderInterface(this)
             .build();

       usersListApi
         .initCall(ApiFactory.getInstance().fetchUsers("1"))
         .execute(true, (response, isError) -> {
           if (!isError)
             adapter.setUserList(response.getData());
         });

  1. With RxJava:
       EasyApiCall<Envelop<List<User>>> usersListApi 
           = new EasyApi.Builder<Envelop<List<User>>(Objects.requireNonNull(getActivity()))
             .setLoaderInterface(this)
             .configureWithRx()
             .build();
      //-----

  1. In Kotlin:
        var usersListApi: EasyApiCall<Envelop<List<User>>>? =
           Builder<Envelop<List<User>>>(activity!!)
           .setLoaderInterface(this)
           .build()

        usersListApi?.initCall(ApiFactory.instance!!.fetchUsers("1"))!!
          .execute(true) { response, isError: Boolean ->
            if (!isError)
              response?.data?.let { adapter?.setUserList(it) }
          }
  1. In Kotlin with Rx:
        var usersListApi: EasyApiCall<Envelop<List<User>>>? =
           Builder<Envelop<List<User>>>(activity!!)
           .setLoaderInterface(this)
           .configureWithRx()
           .build()
        //-----

  1. Cancel or destroy it:
    usersListApi.dispose(); // It will work for both default and Rx-call

  1. Using view model (v 2.0)
public class UserViewModel extends AndroidViewModel {

  private MutableLiveData<List<User>> usersLiveData;
  private MutableLiveData<STATE> loadingStateLiveData;
  private EasyApiCall<Envelop<List<User>>> usersListApi;

  private int pageNo = 1;

  public UserViewModel(Application application) {
    super(application);

    // initialize live data
    usersLiveData = new MutableLiveData<>();
    loadingStateLiveData = new MutableLiveData<>();

    //EasyApi Configuration
    usersListApi = new EasyApi.Builder<Envelop<List<User>>>(Objects.requireNonNull(application))
        .attachLoadingLiveData(loadingStateLiveData).build();
  }

  /**
   * Api call with EasyApi
   * Expose the LiveData Products query so the UI can observe it.
   */
  public void fetchUsers() {
    usersListApi.initCall(ApiFactory.getInstance().fetchUsers(String.valueOf(pageNo)))
        .execute(true, (response, isError) -> {
          if (!isError) {
            usersLiveData.setValue(response.getData());
          }
        });
  }
}

sample_home sample_detail

illustration

Icon made byย Freepikย fromย www.flaticon.comย 


LICENSE

Copyright 2018 Harry's Lab

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.

easy.api's People

Contributors

harintrivedi avatar harintrivedimm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.