Swiftpack.co - GoodRequest/GoodNetworking as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by GoodRequest.
GoodRequest/GoodNetworking 2.3.0
📡 GoodNetworking is an iOS library written in Swift that simplifies HTTP networking by using GRSession and Encodable/DataRequest extensions. It supports latest Swift and all iOS devices, making it a powerful solution for managing network interactions and data encoding/decoding. The library is easy to install with SPM.
⭐️ 31
🕓 5 days ago
iOS
.package(url: "https://github.com/GoodRequest/GoodNetworking.git", from: "2.3.0")

Logo

GoodNetworking

iOS Version Swift Version Supported devices Contains Test Dependency Manager

Handles Swift HTTP networking with the use of various tools such as GRSession, as well as extensions of Encodable and DataRequest. The GRSession provides a powerful and flexible mechanism for managing HTTP sessions, enabling developers to easily handle complex network interactions. Extensions of Encodable and DataRequest provide added functionality for encoding and decoding data, making it easier to handle data transfer between the application and remote servers.

Documentation

Check out GoodNetworking documentation here

Installation

Swift Package Manager

Create a Package.swift file and add the package dependency into the dependencies list. Or to integrate without package.swift add it through the Xcode add package interface.

import PackageDescription

let package = Package(
    name: "SampleProject",
    dependencies: [
        .package(url: "https://github.com/GoodRequest/GoodNetworking" from: "addVersion")
    ]
)

Usage

Define two enums:

  • one for the base API address called ApiServer
  • one that follows the Endpoint protocol, more information here

Create a RequestManager using GRSession

import GoodNetworking
import Combine

enum ApiServer: String {

    case baseURL = "https://api.users.com"

}

enum RequestEndpoint: GREndpointManager {

    // MARK: - User Profile
    
    case userProfile
    
    var path: String { "/user/details/profile" }
    
    var method: HTTPMethod { .get }
    
    var parameters: EndpointParameters? { nil }
    
    var headers: HTTPHeaders? { nil }
    
    var encoding: ParameterEncoding { JSONEncoding.default }

    func asURL(baseURL: String) throws -> URL {
        var url = try baseURL.asURL()
        url.appendPathComponent(path)
        return url
    }

}


class UserRequestManager: UserRequestManagerType {

    // MARK: - Constants

    internal let session: GRSession<RequestEndpoint, ApiServer>

    // MARK: - Initialization

    init(baseURL: String) {
        session = GRSession(
            configuration: .default,
            baseURL: baseURL
        )
    }

    // MARK: - User Profile

    func fetchUserProfile() -> AnyPublisher<ProfileResponse, AFError> {
        return session.request(endpoint: .userProfile)
            .validateToCustomError()
            .goodify()
    }

}

Then inside your viewModel just call

fetchUserProfile()

The result is a publisher so you can continue chaining Combine functions.

License

GoodNetworking is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 31
Last commit: Yesterday
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.3.0
7 weeks ago

What's Changed

Full Changelog: https://github.com/GoodRequest/GoodNetworking/compare/1.2.0...1.3.0

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