Giter VIP home page Giter VIP logo

urlsessiondecodable's Introduction

URLSessionDecodable

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 Paramount. Since Swift Package Manager support in Xcode 11.0, we use it on production in several projects.

Installation

Swift Package Manager

Add to your Package.swift:

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

Usage

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):
	    	   completionHandler(.success(animals))
	    	case .failure(let error):
	    	   let animalsFetchError: ErrorResponse? = error.decodeResponse(using: JSONDecoder())
	    	   completionHandler(.failure(AnimalsError(errorResponse: animalsFetchError)))
	    }
	}
}

Contributing

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

License

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

urlsessiondecodable's People

Contributors

graciborski avatar hello-im-szymon avatar kamilslawinski avatar michallaskowski avatar piotr-naganski-viacomcbs avatar pwszebor-vimn avatar vladkleshchanka avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.