Comments (13)
So you have user generated endpoints?
from moya.
I have endpoints generated by the user of the library, another developer. The user of an app can't define any.
from moya.
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.
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.
A more correct protocol:
protocol EndPoint {
func endPoint() -> String
}
from moya.
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.
You could get better type safety by making the enum
s 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.
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.
Edited the above to reflect that type can be inferred for valid request parameters.
from moya.
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.
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.
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.
Cool. Take a look at #6 and let me know what you think.
from moya.
Related Issues (20)
- Get upload speed? HOT 1
- Can someone teach me that CombineMoya post request is not working. HOT 1
- Update docs RxSwift.md
- How to retry the request with Moya/Combine HOT 1
- Not working on Xcode 15: cannot load module 'Moya' built with SDK 'iphonesimulator16.4' when using SDK 'iphonesimulator17.0 HOT 1
- Cannot call value of non-function type '[Observable<Event>.Type]'
- 'Predicate' is ambiguous for type lookup in this context
- Is this Framework going to be Archived HOT 1
- Hello I have a question about casting from error to Moya error? HOT 5
- Is there any possibility to extract JSON response data when performing a DownloadRequest?
- How to submit type TRUE in post body via Moya
- How can I log the response time / turnaround time in Moya ?
- encoder.encode("1") debugDescription : "Top-level String encoded as string JSON fragment." HOT 1
- Privacy manifest required by Apple HOT 5
- support visionOS? HOT 1
- Upload file starting from a specific byte
- Construct a query string with duplicate keys
- P1: Mandatory Privacy manifest for App store release from May 1 onwards. HOT 2
- Get concrete Request, like DownloadRequest, UploadRequest etc.
- Upload Privacy Policy HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moya.