Swiftpack.co - erikfloresq/RickAndMortyAPI as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by erikfloresq.
erikfloresq/RickAndMortyAPI 0.1.0
Swift wrapper for RickAndMorty API
⭐️ 1
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/erikfloresq/RickAndMortyAPI.git", from: "0.1.0")

RickAndMortyAPI

Swift wrapper for RickandMortyAPI I coded this wrapper with combine

Quick look of methods

Character

getCharacter() -> AnyPublisher<ResponseAPI<Character>, Error>

getCharacter() async throws -> ResponseAPI<Character>

getCharacter(id: String) -> AnyPublisher<Character, Error>

getCharacter(id: String) async throws -> Character

Episode

getEpisode() -> AnyPublisher<ResponseAPI<Episode>, Error>

getEpisode() async throws -> ResponseAPI<Episode>

getEpisode(id: String) -> AnyPublisher<Episode, Error>

getEpisode(id: String) async throws -> Episode

Location

getLocation() -> AnyPublisher<ResponseAPI<Location>, Error>

getLocation() async throws -> ResponseAPI<Location>

getLocation(id: String) -> AnyPublisher<Location, Error>

getLocation(id: String) async throws -> Location

Example

For async await

class ViewModel {
    enum ApiError: Error {
        case failureRequest
    }
    
    let rickAndMortyAPI = RickAndMortyAPI()

    func getCharacter() async throws -> [Characters] {
        do {
            let characters = try await rickAndMortyAPI.getCharacter()
            return characters.results
        } catch {
            throw ApiError.failureRequest
        }
    }
}

For combine

class ViewModel: ObservableObject {
    let rickAndMortyAPI = RickAndMortyAPI()
    @Published var characters = [Character](https://raw.github.com/erikfloresq/RickAndMortyAPI/main/)
    var cancellable = Set<AnyCancellable>()

    func getCharacter() {
        rickAndMortyAPI
            .getCharacter()
            .map { $0.results }
            .sink { (completion) in
                print("")
            } receiveValue: { (characters) in
                self.characters = characters
            }.store(in: &cancellable)
    }
}

GitHub

link
Stars: 1
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics