NetworkRequest

0.1.0

Swift networking library for easily defining, executing, and mocking network requests
yonaskolb/NetworkRequest

What's New

0.1.0

2021-09-02T23:26:29Z

NetworkRequest

A simple networking library for easily defining, executing and mocking network requests.

struct GetPosts: JSONDecodableRequest {  
    let userId: Int

    typealias ResponseType = [Post]
    let path: String = "/posts"
    var urlParams: [String: Any?] { return ["userId": userId] }
}

struct Post: Decodable {
    let userId: Int
    let id: Int
    let title: String
    let body: String
}

let networkService = HTTPNetworkService(baseURL: "https://jsonplaceholder.typicode.com")
let request = GetPosts(userId: 2)

networkService.makeRequest(request) { result in
    switch result {
    case .success(let posts): // posts is [Post]
        print(posts) 
    case .failure(let error): // error is RequestError
        print(error)
    }
}

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Nov 05 2024 07:20:18 GMT-1000 (Hawaii-Aleutian Standard Time)