Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Moya-ObjectMapper with multiple endpoint and same JSON structure #92

Open
sheinix opened this issue Jul 17, 2018 · 0 comments
Open

Comments

@sheinix
Copy link

sheinix commented Jul 17, 2018

Moya version: 'Moya/RxSwift', '~> 11.0' with pod 'Moya-ObjectMapper/RxSwift'

This is a design question about how to use ObjectMapper and Moya the best way. I have multiple endpoints answering with the same JSON structure:

{
    "data": {
                ....
    },
    "metadata": {
        "timestamp": 123232323, 
        "error": null
    }
}

Inside "data" key, is the actual data that is mapped to my model objects. I know I could create each model object with the data and metadata fields and be automatically mapped. But that doesn't seem a good design to me, as "data" and "metadata" aren't part of the model object. I could also use the "map to key" function to map directly what is inside "data", but I'll be missing the metadata field in case there's a reported error from the API.
Is there any way (maybe using protocols?) to not have repeated the data and metadata keys on each of my model objects? I'm making the request with Rx like this:

self.provider.rx.request(MultiTarget(API.global(currency: .usd)))
        .filterSuccessfulStatusCodes()
        .asObservable()
        .mapObject(GlobalData.self)
        .bind(to: globalDataObj) //<-- maybe filtering metadata object before binding?
        .disposed(by: disposeBag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant