All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.stormbit.vksdk.clients.UserClient.kt Maven / Gradle / Ivy

The newest version!
package com.github.stormbit.vksdk.clients

import com.github.stormbit.vksdk.longpoll.LongPoll
import com.github.stormbit.vksdk.objects.Captcha
import com.github.stormbit.vksdk.objects.TwoFactor
import com.github.stormbit.vksdk.objects.upload.Upload
import com.github.stormbit.vksdk.utils.VK_API_VERSION
import com.github.stormbit.vksdk.utils.append
import com.github.stormbit.vksdk.utils.getClientId
import com.github.stormbit.vksdk.vkapi.Auth
import com.github.stormbit.vksdk.vkapi.VkUserPermissions
import com.github.stormbit.vksdk.vkapi.methods.account.AccountApi
import com.github.stormbit.vksdk.vkapi.methods.docs.DocsApi
import com.github.stormbit.vksdk.vkapi.methods.friends.FriendsApi
import com.github.stormbit.vksdk.vkapi.methods.groups.GroupsApi
import com.github.stormbit.vksdk.vkapi.methods.likes.LikesApi
import com.github.stormbit.vksdk.vkapi.methods.messages.MessagesApi
import com.github.stormbit.vksdk.vkapi.methods.photos.PhotosApi
import com.github.stormbit.vksdk.vkapi.methods.users.UsersApi
import com.github.stormbit.vksdk.vkapi.methods.video.VideoApi
import com.github.stormbit.vksdk.vkapi.methods.wall.WallApi
import io.ktor.client.*
import io.ktor.http.*

/**
 * User client, that contains important methods to work with users
 */
class UserClient(
    private val appId: Int = 6222115,
    private val scope: VkUserPermissions = VkUserPermissions().apply { all = true },
    httpClient: HttpClient
) : LoginClient(httpClient) {

    override val longPoll: LongPoll = LongPoll(this)

    override val account: AccountApi = AccountApi(this)
    override val messages: MessagesApi = MessagesApi(this)
    override val groups: GroupsApi = GroupsApi(this)
    override val users: UsersApi = UsersApi(this)
    override val photos: PhotosApi = PhotosApi(this)
    override val video: VideoApi = VideoApi(this)
    override val docs: DocsApi = DocsApi(this)
    override val friends: FriendsApi = FriendsApi(this)
    override val likes: LikesApi = LikesApi(this)
    override val wall: WallApi = WallApi(this)
    override val upload: Upload = Upload(this)

    override suspend fun auth(
        login: String,
        password: String,
        captcha: Captcha?,
        twoFactor: TwoFactor?
    ): Auth.Response {
        val auth = Auth(login, password, httpClient, appId, scope)
        val authResponse = auth.vkLogin(captcha, twoFactor)

        if (authResponse is Auth.Response.Success) {
            token = authResponse.token

            baseParams = Parameters.build {
                append("v", VK_API_VERSION)
                append("access_token", token)
            }
            id = getClientId()

            log.info("Client object created with id $id")

        }

        return authResponse
    }

    override suspend fun auth(token: String): UserClient {
        this.token = token
        baseParams = Parameters.build {
            append("v", VK_API_VERSION)
            append("access_token", token)
        }
        headers = VkUserClient.HEADER
        id = getClientId()

        log.info("Client object created with id $id")

        return this
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy