改造API调用:在进行api调用后如何确保该值不为null?

编程入门 行业动态 更新时间:2024-10-24 02:26:09
本文介绍了改造API调用:在进行api调用后如何确保该值不为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我具有下面的AuthenticationResponse模型,该模型用于使用Retrofit2的3个api调用.如果我拨打verifyEmail,请致电JSON响应主体仅包含有效电子邮件的属性(因此类似{"validEmail":true}).其他2个调用仅包含"resetSuccesful"或其他4个属性.

I have below AuthenticationResponse model that I use for 3 api calls using retrofit2. If I make the verifyEmail call f.e. the JSON response body only contains an attribute for valid email (so something like {"validEmail": true} ). The other 2 calls only contain attributes for "resetSuccesful" or the other 4.

当我收到对verifyEmail呼叫f.e的响应时,如何确定/检查是否正确.包含有效电子邮件的非null值?

How can I make sure/check that when I receive the response to the verifyEmail call f.e. that it contains a non null value for validEmail??

服务:

interface AuthenticationService { @POST("auth/checkEmail") fun verifyEmail(@Body email: String): Call<AuthenticationResponse> @POST("auth/login") fun login(@Body loginCredentials: LoginCredentials): Call<AuthenticationResponse> @POST("auth/resetPassword") fun resetPassword(@Body email: String): Call<AuthenticationResponse> }

型号:

data class AuthenticationResponse( val validEmail: Boolean? = null, val loginSuccess: Boolean? = null, val retriesLeft: Int? = null, val authToken: String? = null, val accountBlocked: Boolean? = null, val resetSuccesful: Boolean? = null)

如果我模拟我的服务器响应以返回f.e. responseCode = 200-{"validEmail":null}并将validEmail类型更改为布尔值(而不是布尔值?)改装不会引发任何异常(这是我真正想要的),因此我的模型给了我一个错误的否定值我的validEmail值.

edit: If i mock my server response to return f.e. responseCode = 200 - { "validEmail": null } and change validEmail type to Boolean (instead of Boolean?) Retrofit doesn't thrown any kind of exception (this is what i actually want) thus my model is giving me a false negative for my validEmail value..

推荐答案

您绝对应该考虑@miensol的注释-为不同的API调用提供单独的模型对象.

You should definitely consider @miensol's comment -- to have separate model objects for different API calls.

但是,如果不可能,则可以使用 Sealed class

However, if that's not possible, you can use Sealed class.

sealed class AuthenticationResponse { data class EmailValidation(val validEmail: Boolean) : AuthenticationResponse() data class SomeSecondResponse(val loginSuccess: Boolean, ...) : AuthenticationResponse() data class SomeThirdResponse(val resetSuccessful: Boolean) : AuthenticationResponse() } fun handleResponse(response: AuthenticationResponse) { when (response) { is AuthenticationResponse.EmailValidation -> response.validEmail is AuthenticationResponse.SomeSecondResponse -> response.loginSuccess is AuthenticationResponse.SomeThirdResponse -> response.resetSuccessful } }

Sealed class是类固醇的枚举-它是具有状态的枚举.您必须为从密封类AuthenticationResponse继承的3个响应创建3个类.

Sealed class is enums on steroids -- it is enums with states. You have to create 3 classes for 3 responses which inherit from the sealed class AuthenticationResponse.

您必须创建与不同的API调用相对应的特定类实例.要访问数据,您可以进行类型检查并访问特定数据.上面的when示例显示了如何访问函数内部的所有响应类型.

You have to create the specific class instance corresponding to the different API calls. To access the data, you can do type check and access the specific data. The above when example shows how to access all the types of response inside a function.

当我收到对以下内容的回复时,如何确定/检查 verifyEmail呼叫f.e.它包含一个非null值,用于 validEmail ??

How can I make sure/check that when I receive the response to the verifyEmail call f.e. that it contains a non null value for validEmail??

由于仅创建特定类的实例,并且所有类仅具有非null属性,因此您不必担心null.

Since you create the instance of only the specific classes and all the classes have only non-null properties, you don't have to worry about null.

更多推荐

改造API调用:在进行api调用后如何确保该值不为null?

本文发布于:2023-10-31 07:25:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不为   API   api   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!