Coder Social home page Coder Social logo

kohttp's Introduction

Kotlin dsl for OkHttp

Build Status Maven Central codecov Codacy Badge Kotlin Awesome Kotlin Badge

Download

gradle kotlin DSL:

compile(group = "io.github.rybalkinsd", name = "kohttp", version = "0.4.0")

gradle groovy DSL:

compile 'io.github.rybalkinsd:kohttp:0.4.0'

maven:

<dependency>
  <groupId>io.github.rybalkinsd</groupId>
  <artifactId>kohttp</artifactId>
  <version>0.4.0</version>
</dependency>

Usage examples

simple sync GET with String.httpGet()

val response: okhttp3.Response = "https://google.com/search?q=iphone".httpGet()

// Response is `AutoClosable` access it with `use` to prevent resource leakage
reponse.use {
    ...
}

simple async GET with String.asyncHttpGet()

This extension starts a new coroutine with Unconfined dispatcher.

val response: Deferred<Response> = "https://google.com/search?q=iphone".asyncHttpGet()

sync GET with httpGet { } dsl

val response: Response = httpGet {
   host = "google.com"
   path = "/search"
   param {
       "q" to "iphone"
       "safe" to "off"
   }
}

reponse.use {
    ...
}

sync GET with header and cookies with httpGet { } dsl

val response: Response = httpGet {
    host = "google.com"
    path = "/search"

    val variable = 123L

    header {
        "one" to 42
        "two" to variable
        "three" to json {
            "a" to variable
            "b" to json {
                "b1" to "512"
            }
            "c" to listOf(1, 2.0, 3)
        }

        cookie {
            "aaa" to "bbb"
            "ccc" to 42
        }
    }

    param {
        "text" to "iphone"
        "lr" to 213
    }
}

reponse.use {
    ...
}

async GET with dsl

@Since 0.4.0

val response: Deferred<Response> = asyncHttpGet {
    host = "google.com"
    path = "/search"
    header { ... }
    param { ... }
}

POST with dsl

Post with form body

form body has a application/x-www-form-urlencoded content type

val response: Response = httpPost {
    host = "postman-echo.com"
    path = "/post"

    param { ... }

    header {
        ...
        cookie { ... }
    }
    
    body {
        form {                              //  Resulting form will not contain ' ', '\t', '\n'
            "login" to "user"               //  login=user&
            "email" to "[email protected]" //  [email protected]
        }
    }
}

reponse.use {
    ...
}

Post with json body

json body has a application/json content type

val response: Response = httpPost {
    host = "postman-echo.com"
    path = "/post"

    param { ... }

    header {
        ...
        cookie { ... }
    }
    
    body {                                  //  Resulting json will not contain ' ', '\t', '\n'
        json {                              //  {
            "login" to "user"               //      "login": "user",
            "email" to "[email protected]" //      "email": "[email protected]" 
        }                                   //  }
    }
}

reponse.use {
    ...
}

Customization

Common Client pool customization

It is possible to customize CommonClientPool by setting up kohttp.yaml in resource directory of your project.

You can check default values in com.kohttp.configuration.Config.kt All time values are in Milliseconds

client:
  connectTimeout: 5000
  readTimeout: 10000
  writeTimeout: 10000
  followRedirects: true
  followSslRedirects: true
  connectionPool:
    maxIdleConnections: 42
    keepAliveDuration: 10000

Run HTTP methods on custom client

TODO

Fork Common Client for specific tasks

TODO

Experimental

Eager response

@Since 0.3.0

Instead of .use { ... it.body?.string() ... } it is now possible to read response body as string. And also to map Headers to listOf<Header> to operate them easily.

val response: EagerResponse = "https://google.com/search?q=iphone".httpGet().eager()

// iterating over headers
response.headers.forEach { ... }

// manipulating body
response.body?.let { ... }
val response: EagerResponse = httpGet { }.eager()

// iterating over headers
response.headers.forEach { ... }

// manipulating body
response.body?.let { ... }

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.