Moya: Is it possible to save or capture Moya responses into stub json ala VCR?

Created on 1 Sep 2017  路  11Comments  路  Source: Moya/Moya

I was wondering if there was a way to automatically capture json responses and put them into JSON. Is there an out of box solution or is one needed to be made?

question stale

Most helpful comment

@AndrewSB So it seems like we both agree this wouldn't be a valuable addition to Moya's core. Better to let other libraries build Moya plugins.

All 11 comments

Hey @apollow. This is a good question. There is no out-of-the-box solution yet (but there is potential for a library). However, I did a demo some time ago to show how to easily save stubs and use them using Moya (and few more), here. This one is kinda old (both Swift/Moya), so you would have to update the syntax, but the idea is there.

Hope it helps!

Kind of similar to #197. I'm not sure if Moya should have a caching layer but maybe this could be extended through a plugin. In v10.0 we're going to review the API around stubbed requests so that would be a good time to look at this further.

@SD10 I'm still pretty bullish on small single purpose libraries, and I think adding caching to Moya would add a lot of complexity and make it harder to reason about the state of program.

Ideal caching in my opinion would sit above Moya, maybe like

let networkProvider = RxMoya<ATarget>()
let diskProvider = SomeDiskProvider()

let cache = ACache(firstPrioritize: diskProvider, thenPrioritize: networkProvider)

cache.giveMeThe(.zen)

What do you think?

I am using Carlos to cache responses. There is an example of how to add Moya support in this issue
It's similar to Andrew's idea.

@AndrewSB I think I had similiar thoughts:

let moyaProvider = MoyaProvider<SomeTarget>()
let cacheProvider = CacheProvider(provider: moyaProvider)

cacheProvider.request(.zen)

I think a cache at any level of a program increases its complexity/the difficulty to reason about its state. Having this hidden in Moya's internals could increase that difficulty but I believe this is true for any library you don't fully understand internally.

My main concerns are:

  • The maintainability of the codebase
  • The quality of a caching feature (considering there are other projects devoted to this)
  • We have to consider different types of persistence

Moya does one simple thing and it does it incredibly well. If a new feature doesn't match the quality of what we do already it's more clutter than anything useful IMO.

@SD10: Other than making it more visible, what upside do you think there would be in building this cache over using Carlos?

@AndrewSB The only upside is Moya users getting these capabilities by default 馃 I don't think we can maintain something as robust as Carlos.

I just checked Alamofire because I consider it a good guide considering we lean so heavily on it and are one level above it. It supports basic caching using URLCache. However, I'm not too fond of the auto-purging behavior 馃槄

I think the capabilities are robust, complicated, and variable enough that we shouldn't prescribe a one-size-fits-all solution to everyone who uses Moya.

I think those who care about caching should configure something that works for them -- whether that be a URLCache, something like PINRemoteImage just to cache images, or Carlos to cache some/all requests -- and all of their caching and expiration policies

@AndrewSB So it seems like we both agree this wouldn't be a valuable addition to Moya's core. Better to let other libraries build Moya plugins.

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 37 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GurpalRajput picture GurpalRajput  路  3Comments

Tynox picture Tynox  路  3Comments

pedrovereza picture pedrovereza  路  3Comments

mwawrusch picture mwawrusch  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments