api refactoring for retrofit2

This commit is contained in:
2021-09-09 12:28:27 +03:00
parent 0588996be8
commit 9a9bde97cb
21 changed files with 622 additions and 18 deletions
@@ -0,0 +1,11 @@
package com.meloda.fast.api.base
import com.google.gson.annotations.SerializedName
import java.io.IOException
data class ApiError(
@SerializedName("error_code")
val errorCode: Int,
@SerializedName("error_msg")
override var message: String
) : IOException()
@@ -0,0 +1,8 @@
package com.meloda.fast.api.base
data class ApiResponse<T>(
val error: ApiError? = null,
val response: T? = null
) {
val isSuccessful get() = error == null && response != null
}
@@ -0,0 +1,130 @@
package com.meloda.fast.api.model
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class BaseVKConversation(
val peer: Peer,
@SerializedName("last_message_id")
val lastMessageId: Int,
@SerializedName("in_read")
val inRead: Int,
@SerializedName("out_read")
val outRead: Int,
@SerializedName("sort_id")
val sortId: SortId,
@SerializedName("last_conversation_message_id")
val lastConversationMessageId: Int,
@SerializedName("is_marked_unread")
val isMarkedUnread: Boolean,
val important: Boolean,
@SerializedName("push_settings")
val pushSettings: PushSettings,
@SerializedName("can_write")
val canWrite: CanWrite,
@SerializedName("can_send_money")
val canSendMoney: Boolean,
@SerializedName("can_receive_money")
val canReceiveMoney: Boolean,
@SerializedName("chat_settings")
val chatSettings: ChatSettings
) : Parcelable {
@Parcelize
data class Peer(
val id: Int,
val type: String,
@SerializedName("local_id")
val localId: Int
) : Parcelable
@Parcelize
data class SortId(
@SerializedName("major_id")
val majorId: Int,
@SerializedName("minor_id")
val minorId: Int
) : Parcelable
@Parcelize
data class PushSettings(
@SerializedName("disabled_forever")
val disabledForever: Boolean,
@SerializedName("no_sound")
val noSound: Boolean,
@SerializedName("disabled_mentions")
val disabledMentions: Boolean,
@SerializedName("disabled_mass_mentions")
val disabledMassMentions: Boolean
) : Parcelable
@Parcelize
data class CanWrite(
val allowed: Boolean
) : Parcelable
@Parcelize
data class ChatSettings(
@SerializedName("owner_id")
val ownerId: Int,
val title: String,
val state: String,
val acl: Acl,
@SerializedName("members_count")
val membersCount: Int,
@SerializedName("friends_count")
val friendsCount: Int,
val photo: Photo,
@SerializedName("admin_ids")
val adminsIds: List<Int>,
@SerializedName("active_ids")
val activeIds: List<Int>,
@SerializedName("is_group_channel")
val isGroupChannel: Boolean,
@SerializedName("is_disappearing")
val isDisappearing: Boolean,
@SerializedName("is_service")
val isService: Boolean
) : Parcelable {
@Parcelize
data class Acl(
@SerializedName("can_change_info")
val canChangeInfo: Boolean,
@SerializedName("can_change_invite_link")
val canChangeInviteLink: Boolean,
@SerializedName("can_change_pin")
val canChangePin: Boolean,
@SerializedName("can_invite")
val canInvite: Boolean,
@SerializedName("can_promote_users")
val canPromoteUsers: Boolean,
@SerializedName("can_see_invite_link")
val canSeeInviteLink: Boolean,
@SerializedName("can_moderate")
val canModerate: Boolean,
@SerializedName("can_copy_chat")
val canCopyChat: Boolean,
@SerializedName("can_call")
val canCall: Boolean,
@SerializedName("can_use_mass_mentions")
val canUseMassMentions: Boolean,
@SerializedName("can_change_style")
val canChangeStyle: Boolean
) : Parcelable
@Parcelize
data class Photo(
@SerializedName("photo_50")
val photo50: String,
@SerializedName("photo_100")
val photo100: String,
@SerializedName("photo_200")
val photo200: String,
@SerializedName("is_default_photo")
val isDefaultPhoto: Boolean
) : Parcelable
}
}
@@ -0,0 +1,47 @@
package com.meloda.fast.api.model
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.RawValue
@Parcelize
data class BaseVKMessage(
val date: Int,
@SerializedName("from_id")
val fromId: Int,
val id: Int,
val out: Int,
@SerializedName("peer_id")
val peerId: Int,
val text: String,
@SerializedName("conversation_message_id")
val conversationMessageId: Int,
@SerializedName("fwd_messages")
val fwdMessages: List<BaseVKMessage> = listOf(),
val important: Boolean,
@SerializedName("random_id")
val randomId: Int,
val attachments: @RawValue List<Any> = listOf(),
@SerializedName("is_hidden")
val isHidden: Boolean,
val payload: String,
val geo: Geo?
) : Parcelable {
@Parcelize
data class Geo(
val type: String,
val coordinates: Coordinates,
val place: Place
) : Parcelable {
@Parcelize
data class Coordinates(val latitude: Float, val longitude: Float) : Parcelable
@Parcelize
data class Place(val country: String, val city: String, val title: String) : Parcelable
}
}
@@ -0,0 +1,16 @@
package com.meloda.fast.api.model.attachments
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class BaseVKAttachmentItem(
val type: String,
val photo: VKPhotoAttachment?,
val video: VKVideoAttachment?,
val audio: VKAudioAttachment?,
val doc: VKFileAttachment?,
val link: VKLinkAttachment?
) : Parcelable
abstract class BaseVKAttachment : Parcelable
@@ -0,0 +1,71 @@
package com.meloda.fast.api.model.attachments
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class VKAudioAttachment(
val id: Int,
val title: String,
val artist: String,
val duration: Int,
val url: String,
val date: Int,
@SerializedName("owner_id")
val ownerId: Int,
@SerializedName("access_key")
val accessKey: String,
@SerializedName("is_explicit")
val isExplicit: Boolean,
@SerializedName("is_focus_track")
val isFocusTrack: Boolean,
@SerializedName("is_licensed")
val isLicensed: Boolean,
@SerializedName("track_code")
val trackCode: String,
@SerializedName("genre_id")
val genreId: Int,
val album: Album,
@SerializedName("short_videos_allowed")
val shortVideosAllowed: Boolean,
@SerializedName("stories_allowed")
val storiesAllowed: Boolean,
@SerializedName("stories_cover_allowed")
val storiesCoverAllowed: Boolean
) : BaseVKAttachment() {
@Parcelize
data class Album(
val id: Int,
val title: String,
@SerializedName("owner_id")
val ownerId: Int,
@SerializedName("access_key")
val accessKey: String,
val thumb: Thumb
) : Parcelable {
@Parcelize
data class Thumb(
val width: Int,
val height: Int,
@SerializedName("photo_34")
val photo34: String,
@SerializedName("photo_68")
val photo68: String,
@SerializedName("photo_135")
val photo135: String,
@SerializedName("photo_270")
val photo270: String,
@SerializedName("photo_300")
val photo300: String,
@SerializedName("photo_600")
val photo600: String,
@SerializedName("photo_1200")
val photo1200: String
) : Parcelable
}
}
@@ -0,0 +1,47 @@
package com.meloda.fast.api.model.attachments
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class VKFileAttachment(
val id: Int,
@SerializedName("owner_id")
val ownerId: Int,
val title: String,
val size: Int,
val ext: String,
val date: Int,
val type: Int,
val url: String,
val preview: Preview?,
@SerializedName("is_licensed")
val isLicensed: Int,
@SerializedName("access_key")
val accessKey: String,
@SerializedName("web_preview_url")
val webPreviewUrl: String?
) : BaseVKAttachment() {
@Parcelize
data class Preview(
val photo: Photo?,
val video: Video?
) : Parcelable {
@Parcelize
data class Photo(val sizes: List<Size>) : Parcelable
@Parcelize
data class Video(
val src: String,
val width: Int,
val height: Int,
@SerializedName("file_size")
val fileSize: Int
) : Parcelable
}
}
@@ -0,0 +1,15 @@
package com.meloda.fast.api.model.attachments
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class VKLinkAttachment(
val url: String,
val title: String,
val caption: String,
val photo: VKPhotoAttachment,
val target: String,
@SerializedName("is_favorite")
val isFavorite: Boolean
) : BaseVKAttachment()
@@ -0,0 +1,32 @@
package com.meloda.fast.api.model.attachments
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class VKPhotoAttachment(
@SerializedName("album_id")
val albumId: Int,
val date: Int,
val id: Int,
@SerializedName("owner_id")
val ownerId: Int,
@SerializedName("has_tags")
val hasTags: Boolean,
@SerializedName("access_key")
val accessKey: String,
val sizes: List<Size>,
val text: String,
@SerializedName("user_id")
val userId: Int?
) : BaseVKAttachment()
@Parcelize
data class Size(
val height: Int,
val width: Int,
val type: String,
@SerializedName("url", alternate = ["src"])
val url: String,
) : Parcelable
@@ -0,0 +1,111 @@
package com.meloda.fast.api.model.attachments
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class VKVideoAttachment(
val id: Int,
val title: String,
val width: Int,
val height: Int,
val duration: Int,
val date: Int,
val comments: Int,
val description: String,
val player: String,
val added: Int,
val type: String,
val views: Int,
@SerializedName("can_comment")
val canComment: Int,
@SerializedName("can_edit")
val canEdit: Int,
@SerializedName("can_like")
val canLike: Int,
@SerializedName("can_repost")
val canRepost: Int,
@SerializedName("can_subscribe")
val canSubscribe: Int,
@SerializedName("can_add_to_faves")
val canAddToFaves: Int,
@SerializedName("can_add")
val canAdd: Int,
@SerializedName("can_attach_link")
val canAttachLink: Int,
@SerializedName("access_key")
val accessKey: String,
@SerializedName("owner_id")
val ownerId: Int,
@SerializedName("ov_id")
val ovId: String,
@SerializedName("is_favorite")
val isFavorite: Boolean,
@SerializedName("track_code")
val trackCode: String,
val image: List<Image>,
@SerializedName("first_frame")
val firstFrame: List<FirstFrame>,
val files: List<File>,
@SerializedName("timeline_thumbs")
val timelineThumbs: TimelineThumbs
//ads
) : BaseVKAttachment() {
@Parcelize
data class Image(
val height: Int,
val width: Int,
val url: String,
@SerializedName("with_padding")
val withPadding: Int
) : Parcelable
@Parcelize
data class FirstFrame(
val height: Int,
val width: Int,
val url: String
) : Parcelable
@Parcelize
data class File(
val mp4_240: String,
val mp4_360: String,
val mp4_480: String,
val mp4_720: String,
val mp4_1080: String,
val mp4_1440: String,
val hls: String,
@SerializedName("dash_uni")
val dashUni: String,
@SerializedName("dash_sep")
val dashSep: String,
@SerializedName("hls_ondemand")
val hlsOnDemand: String,
@SerializedName("dash_ondemand")
val dashOnDemand: String,
@SerializedName("failover_host")
val failOverHost: String
) : Parcelable
@Parcelize
data class TimelineThumbs(
@SerializedName("count_per_image")
val countPerImage: Int,
@SerializedName("count_per_row")
val countPerRow: Int,
@SerializedName("count_total")
val countTotal: Int,
@SerializedName("frame_height")
val frameHeight: Int,
@SerializedName("frame_width")
val frameWidth: Float,
val links: List<String>,
@SerializedName("is_uv")
val isUv: Boolean,
val frequency: Int
) : Parcelable
}
@@ -1,5 +1,6 @@
package com.meloda.fast.api.network
import com.meloda.fast.api.UserConfig
import com.meloda.fast.api.VKConstants
import okhttp3.Interceptor
import okhttp3.Response
@@ -10,6 +11,12 @@ class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val builder = chain.request().url.newBuilder()
.addQueryParameter("v", URLEncoder.encode(VKConstants.API_VERSION, "utf-8"))
UserConfig.accessToken.let {
if (it.isNotBlank())
builder.addQueryParameter("access_token", URLEncoder.encode(it, "utf-8"))
}
return chain.proceed(chain.request().newBuilder().apply { url(builder.build()) }.build())
}
@@ -3,7 +3,9 @@ package com.meloda.fast.api.network
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.meloda.fast.api.network.datasource.AuthDataSource
import com.meloda.fast.api.network.datasource.ConversationsDataSource
import com.meloda.fast.api.network.repo.AuthRepo
import com.meloda.fast.api.network.repo.ConversationsRepo
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@@ -62,4 +64,12 @@ class VKModules {
fun provideAuthDataSource(repo: AuthRepo): AuthDataSource =
AuthDataSource(repo)
@Provides
fun provideConversationsRepo(retrofit: Retrofit): ConversationsRepo =
retrofit.create(ConversationsRepo::class.java)
@Provides
fun provideConversationsDataSource(repo: ConversationsRepo): ConversationsDataSource =
ConversationsDataSource(repo)
}
@@ -0,0 +1,13 @@
package com.meloda.fast.api.network.datasource
import com.meloda.fast.api.network.repo.ConversationsRepo
import com.meloda.fast.api.network.request.ConversationsGetRequest
import javax.inject.Inject
class ConversationsDataSource @Inject constructor(
private val repo: ConversationsRepo
) : ConversationsRepo {
override suspend fun getAllChats(param: ConversationsGetRequest) = repo.getAllChats(param)
}
@@ -1,18 +1,16 @@
package com.meloda.fast.api.network.repo
import com.meloda.fast.api.base.ApiResponse
import com.meloda.fast.api.network.Answer
import com.meloda.fast.api.network.VKUrls
import com.meloda.fast.api.network.response.GetConversationsResponse
import retrofit2.http.*
import com.meloda.fast.api.network.response.ConversationsGetResponse
import com.meloda.fast.api.network.request.ConversationsGetRequest
import retrofit2.http.Body
import retrofit2.http.POST
interface ConversationsRepo {
@FormUrlEncoded
@POST(VKUrls.Conversations.get)
suspend fun getAllChats(
@Field("user_id") chatId: Int,
@Field("token") token: String
): Answer<GetConversationsResponse>
suspend fun getAllChats(@Body param: ConversationsGetRequest): Answer<ApiResponse<ConversationsGetResponse>>
}
@@ -0,0 +1,16 @@
package com.meloda.fast.api.network.request
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class ConversationsGetRequest(
val count: Int? = null,
val offset: Int? = null,
val fields: String = "",
val filter: String = "all",
val extended: Boolean? = true,
@SerializedName("start_message_id")
val startMessageId: Int? = null
) : Parcelable
@@ -1 +0,0 @@
package com.meloda.fast.api.network.request
@@ -1 +1,22 @@
package com.meloda.fast.api.network.response
package com.meloda.fast.api.network.response
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import com.meloda.fast.api.model.BaseVKConversation
import com.meloda.fast.api.model.BaseVKMessage
import kotlinx.parcelize.Parcelize
@Parcelize
data class ConversationsGetResponse(
val count: Int,
val items: List<ConversationsResponseItems>,
@SerializedName("unread_count")
val unreadCount: Int?
) : Parcelable
@Parcelize
data class ConversationsResponseItems(
val conversation: BaseVKConversation,
@SerializedName("last_message")
val lastMessage: BaseVKMessage
) : Parcelable