Modeling request data

We will make use of data classes to model the HTTP request data we wish to send to our API. Go ahead and create a request package in the remote package. There are four obvious requests that contain data payloads, which we will be sending to the API. These are login requests, message requests, status update requests, and requests containing user data. These four requests will be modeled by LoginRequestObject, MessageRequestObject, StatusUpdateRequestObject, and UserRequest objects, respectively.

The following code snippet shows the LoginRequestObject data class. Go ahead and add it to the request package and do the same for other request objects that follow:

package com.example.messenger.data.remote.request

data class LoginRequestObject(
val username: String,
val password: String
)

The LoginRequestObject data class possesses the username and password properties because these are the credentials that need to be supplied to the login endpoint of the API. The MessageRequestObject data class is as follows:

package com.example.messenger.data.remote.request

data class MessageRequestObject(val recipientId: Long, val message: String)

MessageRequestObject possesses two properties as well. These are recipientId—the ID of a user receiving a messageand message the body of the message being sent:

package com.example.messenger.data.remote.request

data class StatusUpdateRequestObject(val status: String)

The StatusUpdateRequestObject data class has a single status property. As the name implies, this is the status that a user wants to update their current status message to:

package com.example.messenger.data.remote.request

data class UserRequestObject(
val username: String,
val password: String,
val phoneNumber: String = ""
)

UserRequestObject is similar to LoginRequestObject with the exception that it contains an additional phoneNumber property. This request object has varying use cases, such as to contain user signup data being sent to the API.

Having created the necessary request objects, we can go ahead and create the actual MessengerApiService.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.111