An

Blog Infos
Author
Published
Topics
Published
Story

Think about having a lot of services on the backend and multiple base urls for our mobile client. How can we handle this? Overwrite the url? Absolutely no. Let’s dive too deep.

In this case, we use a different base url each environment type.

  • alpha → prefix: “alpha-”
  • beta → prefix: “beta-”
  • production → prefix: none

Also, we have multiple services.

  • auth operations → “auth.mydomain.com”
  • pay services → “pay.mydomain.com”
  • app content → “mydomain.com”

We don’t want to specify urls for each retrofit endpoint manually. So, I can use Custom Annotations to manage urls. It makes it easy.

Project Folder Structure

Here is the project folder structure.

We have three retrofit servicesApiType enum class and Api annotation class.

ApiType

We defined an enum type for each microservices. They all have a string for the hostname.

Now we need a flag to manipulate base urls for each retrofit call. We can define an annotation class and set it to each call.

Job Offers

Job Offers


    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocat ...
Google

Jobs

EnvironmentManager & BaseUrlInterceptor

Now we know which call belongs to which service. If we catch this annotation in the interceptor, we’ll do whatever we want. Let’s do it 👇

I wrote an interceptor called BaseUrlInterceptor and a manager called EnvironmentManager which includes environments for base url operations.

Here is my EnvironmentManager. It includes predefined environments and a function for getting url by api type.

We need to catch the Api annotation from the retrofit call.

And we have an annotation and can get the base url from EnvironmentManager by apiType.

Manage Environments Dynamically

Everything is ok. But we want to control these environments dynamically. So, I created a simple selection with spinners. It will be updated environment models when the item is selected.

Firstly, we need to init spinners with our deployment types (alpha, beta, production)

We want to update the environment model when the spinner item is selected. We need to add an item select listener.

And that’s all. Let’s check it.

Think about having a lot of services on the backend and multiple base urls for our mobile client. How can we handle this? Overwrite the url? Absolutely no. Let’s dive too deep.

Now, this article ended too. I hope it will be helpful. I published a sample project with codes on Github. You can see more code details over there. If you find any trouble please write me and we will fix it. Feel free to contact me 🤙

 

Thanks to Omolara Adejuwon

This article was originally published on proandroiddev.com on December 02, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu