Swiftpack.co - ViacomInc/URLSessionDecodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A swift package for adding decoding functionality to URLSession
.package(url: "https://github.com/ViacomInc/URLSessionDecodable.git", from: "0.1.2")


A swift package for adding decoding functionality to URLSession. It is a very small but handful library, that solves the common pattern of deserialization of a response, and fallback on another message format in case of an HTTP error.

We have been using it in some form over the years at former Viacom, now ViacomCBS. Since Swift Package Manager support in Xcode 11.0, we use it on production in several projects.


Swift Package Manager

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/ViacomInc/URLSessionDecodable.git", .upToNextMajor(from: "0.1.0"))


Example usage, fetching a response that has a specific error format if error is handled by a backend:

struct AnimalsResponse: Decodable {
    let name: String

struct AnimalsError: Error {
    // optional, there are also other errors than the ones handled by a backend 
    let errorResponse: ErrorResponse? 

struct ErrorResponse: Decodable {
    let userMessage: String

func getFavoriteAnimals(urlSession: URLSession = .shared,
                        completionHandler: (Result<AnimalsResponse, AnimalsError>) -> Void) -> Cancelable {
	let url = URL(string: "https://myservice.com/favoriteAnimals")!
	return urlSession.decodableTask(with: url,
	                                method: .get,
	                                parameters: nil,
	                                headers: nil,
	                                decoder: JSONDecoder()) { result in
	    switch result {
	    	case .success(let animals):
	    	case .failure(ler error):
	    	   let animalsFetchError: ErrorResponse? = error.decodeResponse(using: JSONDecoder())
	    	   completionHandler(.failure(AnimalsError(errorResponse: animalsFetchError)))


Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.


URLSessionDecodable is released under the Apache 2.0 license. See LICENSE for details.


Stars: 4
Last commit: 6 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

0.1.2 Keeping existing query paramaters
31 weeks ago

Just one change, making sure existing query parameters on url are not removed when adding new via parameterers argument.

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