Skip to content

An elegant tool based on Retrofit to help developers just focus on business data.

License

Notifications You must be signed in to change notification settings

ParadiseHell/convex

Repository files navigation

Convex

Override

Convex is an elegant tool based on Retrofit to help developers just focus on business data.

With the ability of Convex you can process different BaseResponse simply and uniformly.

Background

Lots of Restful APIs' responses are designed as following :

{
	"code" : 0,
	"message" : "",
	"data" : {}
}

or

{
	"status" : 0,
	"message" : "",
	"data" : {}
}

and so on.

So when developers use Retrofit, they have to design a BaseResponse as following :

data class BaseResponse<T>(
	@SerializedName("code")
	val code : Int = 0,
	@SerializedName("message")
	val message : String?,
	@SerializedName("data")
	val data : T?
)

And when define a service method they need to wrap business data with BaseReponse as following :

interface UserService {
	@GET("/users")
	suspend fun getAllUsers() : BaseResponse<List<User>>
}

Thoughts

Handing BaseResponse is really boring and repetitive.😖

Is there a way to just handle BaseResponse only once?🤔

Is there a way to remove the business data wraper BaseResponse?🤔

So Convex comes out.🎉 🎉 🎉

How to use

For more details, please see the ConvexTest or the Android-Project.

Grab Convex from Maven Central

In your build.gradle :

dependencies {
    implementation "org.paradisehell.convex:convex:1.0.0"
}

Implement a ConvexTansformer

private class UserConvexTransformer : ConvexTransformer {
	@Throws(IOException::class)
	override fun transform(original: InputStream): InputStream {
		TODO("Return the business data InputStream.")
	}
}

Add ConvexConverterFactory to Retrofit

Retrofit.Builder()
	.baseUrl("https://users.com/")
	// add ConvexConverterFactory first !!!
	.addConverterFactory(ConvexConverterFactory())
	.addConverterFactory(GsonConverterFactory.create())
	.build()

Define service method with Transformer annotation

interface UserService {
	@GET("/users")
	@Transformer(UserConvexTransformer::class)
	suspend fun getAllUsers() : List<User> // No BaseResponse is needed anymore.👻👻👻
}

That's All, enjoy yourself with Convex.

More convenience usage

As you can see below, every service's method need to annotation with Transformer, which is boring and repetitive. So Convex provide a plugin called convex-booster to automatic to do this work.

Add booster-gradle-plugin and convex-booster to classpath

In your root build.gradle

buildscript {
    dependencies {
        // booster
        classpath "com.didiglobal.booster:booster-gradle-plugin:3.3.1"
        // convex-booster
        classpath "org.paradisehell.convex:convex-booster:1.0.0"
    }
}

Apply booster plugin

In your app build.gradle

plugins {
    id 'com.didiglobal.booster'
}

or

apply plugin: "com.didiglobal.booster"

Define service with Transformer annotation

@Transformer(UserConvexTransformer::class)
interface UserService {
	@GET("/users")
	suspend fun getAllUsers() : List<User> // No BaseResponse is needed anymore.👻👻👻
}

If you do not want ConvexTransformer to work with some service methods, you can use DisableTransformer annotation with these method as following.

@Transformer(UserConvexTransformer::class)
interface UserService {
	@GET("/users")
	@DisableTransformer // Convex will ignore UserConvexTransformer
	suspend fun getAllUsers() : BaseResponse<List<User>>
}

Thanks

  • booter
    • Optimizer for mobile applications.🚀🚀🚀
    • An elegant framework to process bytecode really simply.👍👍👍

License

Copyright 2021 ParadiseHell.

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.

About

An elegant tool based on Retrofit to help developers just focus on business data.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages