Git Product home page Git Product logo

retrogrid's Introduction

What is Retrogrid

Retrogrid is a library built on top of retrofit library for the easy intergation of your application.

Steps to integrate

  1. Add it in your root build.gradle at the end of repositories:
allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

2. Add the dependency in your app module
dependencies {
	        implementation 'com.github.DeepuGeorgeJacob:retrogrid:v1.0.2'
	}

Great you are done with integration.


How to use the libary.

There are two ways(property file and annotation) to use this this library. Easy way is by creating annotation

By Annotation

  1. Create your repository like below
@RetrofitServiceConfiguration(baseUrl = "http://example.com/v1/")
interface RetrogridService {
    @GET(value = "method_url")
    @ErrorResponseMap(errorClass = NewsErrorResponse::class)
    suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>
}

@RetrofitServiceConfiguration is the annotation that you can add your base url.
@ErrorResponseMap Which you can mention your class which hold error response. Error Response will be different for different project your back end team define the error structure.
Eg:-

data class NewsErrorResponse(
    private val status: String,
    private val code: String,
    private val message: String
)

Make sure your method to get the data is suspended function.

Eg:-

suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>

RetrogridResponse is the class which containing your response object or available error objects if available. You should pass your success response class as typed argument. Here NewsOverview is my response data class.

Eg:->

data class NewsOverview(
    val status: String,
    val totalResults: Int,
    val articles:List<String>
)
  1. Create your repository like this.
class NewsRepository(
    private val service: RetrogridService = RetroGridNetworkClient.buildService(RetrogridService::class.java)
) {
    suspend fun getNewsOverView() = service.fetchNewsOverview()
}


Congratulations you are done with 90%

  1. Call the api from your viewmodel.
class RetrogridViewModel(private val repository: NewsRepository = NewsRepository()) : ViewModel() {

    init {
        viewModelScope.launch {
            when (val result = repository.getNewsOverView()) {
                is RetrogridResponse.Success -> println("SUCCESS ${result.responseBody}")
                is RetrogridResponse.Failure -> {
                    println((result.errorResponseBody as NewsErrorResponse).toString())
                }
                is RetrogridResponse.NetworkError -> println("Network error : "+result.errorMessage)
                is RetrogridResponse.UnknownError -> println("Unknown error : "+result.errorMessage)
            }
        }

    }
}

Notice how you should handle success and error responses.

You did it!! Contratulations :)

Additional configuration (Global base_url using properties file)

If you have to add static base url, Please follow the below steps.

  1. You should create a file called retrofit.properties in the src/main/resources directory of the app module.
  2. In the app module's build.gradle file, add the following lines to make sure the properties file is included in the build:
android {
    // ...
    sourceSets {
        main {
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
}
  1. Add the property value with key api.base.url in your retrofit.properties file. Example:- api.base.url=https://example.org/v2/

  2. Now you can use service without @RetrofitServiceConfiguration annotation.

@RetrofitServiceConfiguration(baseUrl = "https://example.com/v1/")

interface RetrogridService {
    @GET(value = "method_url")
    @ErrorResponseMap(errorClass = NewsErrorResponse::class)
    suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>
}


Note : If you configure with both annotation and retrofit.properties file, then annotation will override base url value. Which mean if you want to change the base url for any specific services you should use @RetrofitServiceConfiguration annotation.

Happy coding :) Contact the author by this link

retrogrid's People

Contributors

deepugeorgejacob avatar

Watchers

 avatar

retrogrid's Issues

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.