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

Documentation lacks examples using Codable structs #2

Closed
djrobby opened this issue Jul 10, 2020 · 3 comments
Closed

Documentation lacks examples using Codable structs #2

djrobby opened this issue Jul 10, 2020 · 3 comments

Comments

@djrobby
Copy link

djrobby commented Jul 10, 2020

I would really like to use your library in my current project but I'm having a real hard time getting POST, PUT and DELETE requests to work with Codable structs. Your documentation, including tests, only showcase rather simple examples that use strings as params. It would be nice if you could include addtional examples utilizing Codable structs. Thanks!

@s4cha
Copy link
Member

s4cha commented Jul 22, 2020

Hi @djrobby, indeed the examples in the tests are quite succinct. For an example in a real Appstore App you could check out the code of this fully open-source App :https://github.com/EuroScorer/EuroScorer-iOS
especially https://github.com/EuroScorer/EuroScorer-iOS/blob/master/EuroScorer/Firebase/FirebaseModels.swift and https://github.com/EuroScorer/EuroScorer-iOS/blob/master/EuroScorer/Firebase/FirebaseImplementation.swift

The particularity in this example is that Codable Models have been hidden behind a protocol for cleanness sake but you don't have to go to this level of abstraction if you don't want to. This technique enables us to hide away Firebase and the JSON api from the rest of the app.

// Your Model (Simplified)
struct FirebaseSong: Song, Decodable {
    let title: String

    enum CodingKeys: String, CodingKey {
        case title = "title"
    }
}

// Tell Netwoking that its JSONDecodable (its empty since implementation is provided by default for Codables)
extension FirebaseSong: NetworkingJSONDecodable {}

// Perform your call
 func fetchSongs() -> AnyPublisher<[Song], Error> {
    get("/songs").map { (firebaseSongs: [FirebaseSong]) -> [Song] in
        firebaseSongs.map { $0 as Song }
    }.eraseToAnyPublisher()
}

Let me know if that helps :)

@s4cha
Copy link
Member

s4cha commented Nov 30, 2020

@djrobby Did that help?

@djrobby
Copy link
Author

djrobby commented Dec 3, 2020

Yes, all set! Sorry, I forgot to mark the issue closed. done!

@djrobby djrobby closed this as completed Dec 3, 2020
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

2 participants