I have large sample response for each endpoint in my app. However, I am afraid that if I directly paste the response inside each switch case my code will start looking ugly. I was wondering what are the other was to do this:
Please find below part of my switch statement with sample response for the smallest case.
var sampleData: Data {
switch self {
case .movieDetails(_):
return "{\"adult\": false,\"backdrop_path\": \"/wSJPjqp2AZWQ6REaqkMuXsCIs64.jpg\",\"belongs_to_collection\": null,\"budget\": 63000000,\"genres\": [{\"id\": 18,\"name\": \"Drama\"}],\"homepage\": \"http://www.foxmovies.com/movies/fight-club\",\"id\": 550,\"imdb_id\": \"tt0137523\",\"original_language\": \"en\",\"original_title\": \"Fight Club\",\"overview\": \"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.\",\"popularity\": 6.868575,\"poster_path\": \"/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg\",\"production_companies\": [{\"name\": \"Regency Enterprises\",\"id\": 508},{\"name\": \"Fox 2000 Pictures\",\"id\": 711},{\"name\": \"Taurus Film\",\"id\": 20555},{\"name\": \"Linson Films\",\"id\": 54050},{\"name\": \"Atman Entertainment\",\"id\": 54051},{\"name\": \"Knickerbocker Films\",\"id\": 54052}],\"production_countries\": [{\"iso_3166_1\": \"DE\",\"name\": \"Germany\"},{\"iso_3166_1\": \"US\",\"name\": \"United States of America\"}],\"release_date\": \"1999-10-14\",\"revenue\": 100853753,\"runtime\": 139,\"spoken_languages\": [{\"iso_639_1\": \"en\",\"name\": \"English\"}],\"status\": \"Released\",\"tagline\": \"How much can you know about yourself if you've never been in a fight?\",\"title\": \"Fight Club\",\"video\": false,\"vote_average\": 8.1,\"vote_count\": 5863}".data(using: String.Encoding.utf8)!
case .similarMovies(_):
I agree it can get ungainly fast. I would recommend loading the data from files instead of including it directly in your TargetType. I'm going to work on including some helper functions in TargetType for this.
Another option you have right now is to put the sampleData into a separate file.
For instance, create a GitHub+SampleData.swift that contains just this:
extension GitHub {
var sampleData: Data {
// put your sample data here
}
}
That file would still be pretty ugly, but it would clean up the main API code.
Agreed, it can get pretty wild. I store sample response data (JSON mostly) in files and return the contents of those files instead of hard-coding things. Here's an example: https://github.com/artsy/eidolon/blob/bbaa513cc50e6bdfe0aad317ffca1372ecbc8323/Kiosk/App/Networking/ArtsyAPI.swift#L182-L238 Let us know how it goes 馃憤
Thanks @scottrhoyt @ashfurrow . This is indeed a great idea. @ashfurrow your implementation is really elegant. Planning to implement it in the same way
Closing this now then, let us know if you encounter any issues!
Most helpful comment
Agreed, it can get pretty wild. I store sample response data (JSON mostly) in files and return the contents of those files instead of hard-coding things. Here's an example: https://github.com/artsy/eidolon/blob/bbaa513cc50e6bdfe0aad317ffca1372ecbc8323/Kiosk/App/Networking/ArtsyAPI.swift#L182-L238 Let us know how it goes 馃憤