Skip to content

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

@sheinix

Description

@sheinix

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions