Giter VIP home page Giter VIP logo

Comments (13)

tLewisII avatar tLewisII commented on May 16, 2024

So you have user generated endpoints?

from moya.

ashfurrow avatar ashfurrow commented on May 16, 2024

I have endpoints generated by the user of the library, another developer. The user of an app can't define any.

from moya.

michaelmcguire avatar michaelmcguire commented on May 16, 2024

Can you make the MoyaProvider generic and have it take an enum as a parameter? Then you can verify the endpoint definition take one of those enums and the request method take one of the enums. The user of the library would still need to associate each enum value with an endpoint... Not sure best way to do that.

from moya.

tLewisII avatar tLewisII commented on May 16, 2024

You could do two sort of things, if I am understanding correctly. Have an enum that defines every endpoint, or pieces of an endpoint, which may be super huge, something like:

enum EndpointPiece:String {
    case Base = "http://rdjpg.com/"
    case TwoHundred = "200/"
    case ThreeHundred = "300/"
    ....
}

And then have methods to concatenate them to form an actual endpoint. This may give you the closest thing to type safety.

Or you could have a protocol like so:

protocol EndPoint {
    typealias E
    func endPoint(point:String) -> E
}

And then only take objects conforming to the EndPoint protocol, so endpoints would at least have to be constructed out of structs or objects, and not just defined as strings all over the place.

from moya.

tLewisII avatar tLewisII commented on May 16, 2024

A more correct protocol:

protocol EndPoint {
    func endPoint() -> String
}

from moya.

paulyoung avatar paulyoung commented on May 16, 2024
struct Endpoint<T> {
    let URL: T
}

struct MoyaProvider<T: RawRepresentable> {
    let EndpointType: T.Type
    let endpoints: [Endpoint<T>]
    func request(URL: T) {
        println(URL.toRaw())
    }
}

enum RdJpg:String {
    case ThreeHundredByTwoHundred = "http://rdjpg.com/300/200/"
}

enum Foo:String {
    case Bar = "http://foo.com/bar"
    case Baz = "http://foo.com/baz"
}

let rdJpgEndpoint = Endpoint(URL: RdJpg.ThreeHundredByTwoHundred)
let fooEndpoint = Endpoint(URL: Foo.Bar)

// Valid providers
let rdJpgProvider = MoyaProvider(EndpointType: RdJpg.self, endpoints: [rdJpgEndpoint])
let fooProvider = MoyaProvider(EndpointType: Foo.self, endpoints: [fooEndpoint])

// Invalid providers
//let rdJpgProvider = MoyaProvider(EndpointType: RdJpg.self, endpoints: [fooEndpoint])
//let fooProvider = MoyaProvider(EndpointType: Foo.self, endpoints: [rdJpgEndpoint])

// Valid requests
rdJpgProvider.request(RdJpg.ThreeHundredByTwoHundred)
fooProvider.request(Foo.Bar)
fooProvider.request(Foo.Baz)

// Invalid requests
//rdJpgProvider.request(Foo.Bar)
//rdJpgProvider.request(Foo.Baz)
//fooProvider.request(RdJpg.ThreeHundredByTwoHundred)

from moya.

paulyoung avatar paulyoung commented on May 16, 2024

You could get better type safety by making the enums conform to a protocol like this:

protocol EndpointURL: RawRepresentable {
  class func fromRaw(raw: String) -> Self?
}

enum RdJpg:String, EndpointURL {
    case ThreeHundredByTwoHundred = "http://rdjpg.com/300/200/"
}

from moya.

paulyoung avatar paulyoung commented on May 16, 2024

Altogether now:

protocol EndpointURL: RawRepresentable {
    class func fromRaw(raw: String) -> Self?
}

struct Endpoint<T> {
    let URL: T
}

struct MoyaProvider<T: EndpointURL> {
    let EndpointType: T.Type
    let endpoints: [Endpoint<T>]
    func request(URL: T) {
        println(URL.toRaw())
    }
}

enum RdJpg:String, EndpointURL {
    case ThreeHundredByTwoHundred = "http://rdjpg.com/300/200/"
}

enum Foo:String, EndpointURL {
    case Bar = "http://foo.com/bar"
    case Baz = "http://foo.com/baz"
}

let rdJpgEndpoint = Endpoint(URL: RdJpg.ThreeHundredByTwoHundred)
let fooEndpoint = Endpoint(URL: Foo.Bar)

// Valid providers
let rdJpgProvider = MoyaProvider(EndpointType: RdJpg.self, endpoints: [rdJpgEndpoint])
let fooProvider = MoyaProvider(EndpointType: Foo.self, endpoints: [fooEndpoint])

// Invalid providers
//let rdJpgProvider = MoyaProvider(EndpointType: RdJpg.self, endpoints: [fooEndpoint])
//let fooProvider = MoyaProvider(EndpointType: Foo.self, endpoints: [rdJpgEndpoint])

// Valid requests
rdJpgProvider.request(.ThreeHundredByTwoHundred)
fooProvider.request(.Bar)
fooProvider.request(.Baz)

// Invalid requests
//rdJpgProvider.request(Foo.Bar)
//rdJpgProvider.request(Foo.Baz)
//fooProvider.request(RdJpg.ThreeHundredByTwoHundred)

from moya.

paulyoung avatar paulyoung commented on May 16, 2024

Edited the above to reflect that type can be inferred for valid request parameters.

from moya.

roop avatar roop commented on May 16, 2024

Here is one possible solution:

// Defined in Moya:

protocol StringConvertible {
    func toString() -> String
}

class Endpoint<T: StringConvertible> {
    let baseURL: String
    let slug: T
    init(baseURL: String, slug: T) {
        self.baseURL = baseURL
        self.slug = slug
    }
    func fullURLString() -> String {
        return self.baseURL + self.slug.toString()
    }
}

// So a user of Moya can write:

enum Slug: String, StringConvertible {
    case DoTaskOne = "/do/task1"
    case DoTaskTwo = "/do/task2"
    // Unfortunately, the following method has to be
    // defined by all Moya users
    func toString() -> String {
        return self.toRaw()
    }
}

var endpoint = Endpoint<Slug>(baseURL: "http://example.com", slug: .DoTaskOne)
println(endpoint.fullURLString())

from moya.

roop avatar roop commented on May 16, 2024

Hey, looks like it's also possible to get rid of the toString() requirement:

// Defined in Moya
protocol StringBackedEnum {
    func toRaw() -> String
}

and then:

// Code using Moya
enum Slug: String, StringBackedEnum {
    case DoTaskOne = "/do/task1"
    case DoTaskTwo = "/do/task2"
}

Personally, I like the StringConvertible way better, because the API contract is clearer there, methinks.

from moya.

ashfurrow avatar ashfurrow commented on May 16, 2024

I really, realy dig these. We could even go further and abstract away the need for a URL string, which would decouple its 1-1 relationship with an endpoint. That way, you could provide a different URL depending on parameters, etc. I'll put together a pull request.

from moya.

ashfurrow avatar ashfurrow commented on May 16, 2024

Cool. Take a look at #6 and let me know what you think.

from moya.

Related Issues (20)

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.