0

The application was working, but then something changed in the structure of the response and during authorization (login + universal token) is not included in the application

code:

suspend fun authorize(phone: String, password: String) : AccessTokenResponse? {

        //safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
        val response = safeApiCall(
            call = {api.authorize(AuthorizeRequest(phone = phone, password = password)).await()},
            errorMessage = "Error Fetching Popular Movies"
        )

        return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
    }

    suspend fun accessToken(authorizationCode: String) : AccessTokenResponse? {

        //safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
        val response = safeApiCall(
            call = {api.accessToken(AccessTokenRequest(authorizationCode = authorizationCode)).await()},
            errorMessage = "Error Fetching Popular Movies"
        )

        return response?.data?.items?.getOrNull(0)
    }

namely, it gives an error on the line:

return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)

Structure AccessTokenResponse:

data class AccessTokenResponse(
    @SerializedName("access_token")
    val accessToken: String,

    @SerializedName("expires_at")
    val expiresAt: Long,

    @SerializedName("refresh_token")
    val refreshToken: String,

    @SerializedName("refresh_expires_at")
    val refreshExpiresAt: Long
)

Structure AuthorizeResponse:

data class AuthorizeResponse(
    @SerializedName("authorization_code")
    val authorizationCode: String,

    @SerializedName("expires_at")
    val expiresAt: Long
)

Console:

W/System.err: java.lang.NullPointerException
        at com.example.ferry.data.api.FerryRepository.authorize(FerryRepository.kt:29)
        at com.example.ferry.data.api.FerryRepository$authorize$1.invokeSuspend(Unknown Source:12)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

json:

{
        "result": true,
        "errors": null,
        "items": [
            {
                "user": [
                    {
                       ...
                    }
                ],
                "authorization_code": "1234352353245dsfasfasfdasf",
                "expires_at": 1620982345
            }
        ],
        "versions": {
           ...
        }
    }

I would be very grateful for your help

Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30
Rudione
  • 1
  • 1

1 Answers1

0

You have to handle the null exception there. For example you can approach something like this. I haven't try this code. I hope you get the point.

From there, when you want to use this suspend fun, you need to check either this fun is null or not, again.

if (response != null) {
     return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
}
return null
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46