ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - mobiletoly/urlsome: URLSome - URL builder library for Kotlin
URLSome - URL builder library for Kotlin. Contribute to mobiletoly/urlsome development by creating an account on GitHub.
Visit Site

GitHub - mobiletoly/urlsome: URLSome - URL builder library for Kotlin

GitHub - mobiletoly/urlsome: URLSome - URL builder library for Kotlin

URLSome

URLSome - URL builder library for Kotlin

Add to gradle build

The easiest way to add this library to your gradle build is to use jitpack

repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://jitpack.io' }
    // ...
}

dependencies {
    // ...
    implementation group: "com.github.mobiletoly", name: "urlsome", version: "0.4-SNAPSHOT"
}

How to use

To build this URL:

http://127.0.0.1:8081/api/v1/users/Toly+Pochkin/report?sort=firstName&country=US#main

use this code:

val reportUser = "Toly Pochkin"
val baseUrl = Urlsome("http://127.0.0.1:8081/api/v1")
val url = (baseUrl/"users"/reportUser/"report") [
    "sort" to "firstName",
    "country" to "US"
] `#` "main"
println(url.toString())

Urlsome() accepts base URL that not going to be URL encoded. Then / is used to append and path components. This path components will be URL encoded, e.g. if you perform ("api"/"user"/"/hello") then resulting URL will be "api/user/%2Fhello". If you don't want to encode path component then use * operator instead of /. You can mix both operators if needed, e.g. ("api"/"user"*"/hello") will result in "api/user//hello".

After you done passing path components - use square brackets to pass pairs of key/values for query parameters. If by some reasons you need to add fragment component - use # infix function. Last step is to call url.toString() to build URL and convert it to a string.

Another way to get the same URL is:

val url = (
     (baseUrl/"users"/reportUser/"report")
    `?` ("sort" to "firstName")
    `?` ("country" to "US")
    `#` "main"
)

Or even more fancy:

val url = ((baseUrl/"users"/reportUser/"report")
    `?` ("sort" to "firstName") `&` ("country" to "US") `#` "main")

Actually there is no difference between ? and &, both of them can be used to append query parameters, but & a little more readable when passing multiple parameters, because it looks like a typical query parameters separator.

Take a look at unit tests for more examples.

Usage with other libraries

We don't really need integration with any of the existing HTTP libraries, by the end of the day Urlsome class always gets transformed to String. But feel free to simplify your use of your favorite HTTP library by using extension functions to support Urlsome instances. For example, let's take a look at popular library called Fuel. Your typical call with Fuel by using Urlsome instance will look like this:

private val serviceUrl = Urlsome("https://......")
// ...
suspend fun getSomething(): Something {
    return Fuel.get((serviceUrl/"admin"/"users" ["payGrade" to 7]).toString())
        .awaitObject(SomeDeserializer)
}

This is boring since you have to append .toString() call all the time. So just create a few extension functions instead, e.g.

fun Fuel.Companion.get(url: Urlsome) = get(url.toString())
fun Fuel.Companion.post(url: Urlsome) = post(url.toString())
// ...

much better now:

suspend fun getSomething(): Something {
    return Fuel.get(serviceUrl/"admin"/"users" ["payGrade" to 7])
        .awaitObject(SomeDeserializer)
}

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory