GithubHelp home page GithubHelp logo

bnorm / ktor-retrofit Goto Github PK

View Code? Open in Web Editor NEW
132.0 132.0 7.0 107 KB

Turns a Retrofit service interface into Ktor routing

License: Apache License 2.0

Kotlin 100.00%
kotlin ktor ktor-framework retrofit retrofit2

ktor-retrofit's Introduction

I do the clickety-clackety that make the computer go brrr

ktor-retrofit's People

Contributors

bnorm avatar joshafeinberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ktor-retrofit's Issues

Support Response<T> return type serialization in APIServices

I use Response wrapped response structure everywhere in my services (for error handling) so it looks like:

interface Service {
  @GET("string")
  suspend fun getAll(): Response<List<String>>

  @GET("string/{id}")
  suspend fun getSingle(@Path("id") id: Long): Response<String>
}

And then in the ktor-retrofit service implementation, I then return a

return Response.success(myResponseObj)

But for some reason, my responses are not transmitted if implemented this way, here's a snippet of the response when hitting a ktor-retrofit implemented endpoint (logged via HttpLoggingInterceptor):

Vary: Origin
Content-Type: application/json; charset=UTF-8
Connection: keep-alive
transfer-encoding: chunked

{}
<-- END HTTP (2-byte body)

Notice the empty {}

The service is installed as:

  routing {
        route("/") {
            authenticate(AuthLabel) {
                retrofitService(service = MyService)
            }

Protect against service interfaces with conflicting endpoints

The behavior of multiple functions in a service mapping to the same endpoint is currently unknown. Retrofit probably doesn't need to protect against this as there could be multiple forms for the same endpoint on the client side. However, installing multiple handlers for the same endpoint might lead to confusing behavior on the server side and should probably not be allowed.

Support multiple interfaces in service

Right now you only support services that have a single super class

https://github.com/bnorm/ktor-retrofit/blob/master/ktor-retrofit/src/main/kotlin/com/bnorm/ktor/retrofit/RetrofitService.kt#L59

Is there a technical reason for this? I have a branch running it fine without that check and as of Retrofit 2.7 it supports this as well.

Example (this currently crashes)

interface Network: NetworkOne, NetworkTwo

class Services(
    networkOne: NetworkOne,
    networkTwo: NetworkTwo
) : Network,
    NetworkOneby networkOne,
    NetworkTwoby networkTwo

retrofitService(service = Services(networkOne, networkTwo))

How to wrap the service inside authenticate{}?

I'd like to use io.ktor.auth's authentication and just stick the implementation of ktor-retrofit Service inside the authenticate{} block.

What's the recommended way to do this?

Something like this?

            authenticate("myService") {
                install(RetrofitService) {
                    service(service = MyService)
                }
            }

This doesn't look right and for having multiple services I'll need to do install() multiple times. What's the right way?

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.