GithubHelp home page GithubHelp logo

ktorclient's Introduction

KtorClient

Why Ktor on client-side?

Ktor is built on Kotlin multi-platform mobile (KMM). This means you can create both iOS and Android applications with Kotlin and share a huge part of Kotlin code for both platforms.

Add Dependencies and Permissions

add internet permission in AndroidManifest.xml. required to make HTTP requests to the API

<uses-permission android:name="android.permission.INTERNET"/>

in build.gradle(:app)

dependencies {
    ...
    def ktor_version = "1.6.3"
    // adds Ktor's core components to our project.
    implementation "io.ktor:ktor-client-core:$ktor_version"
    
    // HTTP engine: The HTTP client used to perform network requests, for iOS, we would use an iOS dependency.
    implementation "io.ktor:ktor-client-android:$ktor_version"
    
    // convert objects to and from JSON.
    implementation "io.ktor:ktor-client-serialization:$ktor_version"
    
    //Logging
    implementation "io.ktor:ktor-client-logging:$ktor_version"
    // allows us to see nicely formatted logs in a console.
    implementation "ch.qos.logback:logback-classic:1.2.3"

    // provides a convenient mechanism for converting Kotlin objects into a serialized form like JSON, and vice vers
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
}

apply plugin for kotlinx-serialization

plugins {
    ...
    id 'org.jetbrains.kotlin.plugin.serialization'
    ...
}

add classpath in build.gradle project file

dependencies {
        classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.21"
}

Setup the data models

  • A model is a data transfer object(dto). It has data classes that represent what we get from an API.
  • inside remote.data.dto package create new data class called PostResponse, and create another one for requests.
  • we'll get data from this API

Here's how the PostResponse will look like:

@Serializable
data class PostResponse(
    val body: String,
    val title: String,
    val id: Int,
    val userId: Int,
)

Setup the API

  1. setup the api endpoint
  • create a new Kotlin Object file named HttpRoutes
object HttpRoutes {
    private const val BASE_URL = "https://jsonplaceholder.typicode.com"
    const val POSTS = "$BASE_URL/posts"
}
  1. define which functions we need to access the endpoints.
  • create PostsService interface that will have mainly 2 functions (getPosts() and createPost(postRequest))
  • create PostsServiceImpl to implement the actual network call using the Ktor client Impl

Reference

ktorclient's People

Contributors

salma-2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.