RRR
RRR

Reputation: 509

android and REST API - architecture to manage user and requests to server

I have made a REST API for an app I have. I have secured it with JWT bearer tokens. I have made a website and an android app for this application

The website is made with ReactJS. I used this pattern to handle users and requests:

  1. use reducers and actions to update state
  2. use middleware before every request to check token age, and refresh if needed
  3. use high order components to check if user is eligible to get to a certain route

related items are put in one place.

Now to the android app ..

  1. My requests are done via okhttp in a method inside the activity class that uses it. This is quite buggy because sometimes I need to use this same method outside of that activity. I tried making a Request class but then I couldn't use it and making it static will not make me be able to use runOnUiThread
  2. user is saved in sharedpreferences. BUT I don't thing accessing shared preferences each time I need user is optimal. I made a static field user in main activity. and all other activities which needs it can access it with no problem. still buggy

isn't there a better way to manage all of this? my code at the moment runs but it's pretty much spagetti. Plus there are some functions which I would like to implements before each request like checking the token and refreshing it if needed or logging out the user if token is expired. I looked around but didn't see any thread talking about android REST API architecture

tl;dr: my android architecture for handling the user and requests now works. but it is very buggy. How can I improve this? are there already made architectures that I can implement?

Upvotes: 0

Views: 207

Answers (1)

Blundell
Blundell

Reputation: 76476

You can do a redux architecture on Android to get your app a little bit more similar to the ReactJs site https://jayrambhia.com/blog/android-redux-intro

For middle ware you will probably want to use Interceptors, tho I realise that doesn't help with architecture https://square.github.io/okhttp/interceptors

For higher order components to check user eligiblity, "use cases" come to mind in clean architecture, you can read this https://proandroiddev.com/kotlin-clean-architecture-1ad42fcd97fa or just good "clean architecture android"

enter image description here

Upvotes: 3

Related Questions