Swiftpack.co - Valerio69/SmartNet as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Valerio69/SmartNet
Swift Networking Library
.package(url: "https://github.com/Valerio69/SmartNet.git", from: "0.0.8")

SmartNet

HTTP Networking Library which primary scope is to make Networking as easy as possible. Greatly inspired by SENetworking (https://github.com/kudoleh/SENetworking).

Features

  • Easy configuration
  • Supported Requests: Encodable, Dictionary, String
  • [Supported Response Types: Decodable, String, Data, Void
  • Supported QueryParameters: Encodable, Dictionary
  • iOS 13+ Combine Support
  • Light and easy Networking Interface
  • "Truested Domains" list to bypass SSL Authentication Challenge (not recommended)

Installation

Swift Package Manager

pod 'SmartNet'

Examples

  • Network Default Configuration

NetworkConfiguration is used to define defaults settings that are going to be used in every call.

If the Endpoint is initialized with "useEndpointHeaderOnly: true" the NetworkConfiguration headers are going to be ignored.

Base

let config = NetworkConfiguration(baseURL: URL(string: "https://api.example.com")!)
let network = SmartNet(config: config)

Advanced

let config = NetworkConfiguration(
    baseURL: URL(string: "https://api.publicapis.org")!,
    headers: ["Content-Type": "application/json"],
    queryParameters: ["userid": "xxxxxx"],
    trustedDomains: ["api.publicapis.org"],
    requestTimeout: 120
)
let network = SmartNet(config: config)
  • Create an Endpoint

GET

let endpoint = Endpoint<Person>(
    path: "person",
    queryParameters: QueryParameters(
        parameters: [
            "name": "Jhon", 
            "age": 18
        ]
    )
)

Equivalent of https://api.example.com/person?name=Jhon&age=18

POST

let endpoint = Endpoint<Person>(
    path: "person",
    method: .post,
    body: HTTPBody(
        encodable: PersonRequst(
            name: "Jhon",
            age: 18
        )
    )
)

Equivalent of https://api.example.com/person with body equal to:

{
    "name": "Jhon",
    "age": 18
}

API CALL

  • Using Closures
network.request(with: endpoint) { (response) in
    switch response {
    case .success(let person):
        print("Success! \(person.name)")
    case .failure(let error):
        print(error.localizedDescription)
    }
}
  • Using Combine
var subscriptions = Set<AnyCancellable>()

network.request(with: endpoint)?
    .sink(
        receiveCompletion: { (response) in
            if case .failure(let error) = response {
                print(error.localizedDescription)
            }
        },
        receiveValue: { (person) in
            print("Success! \(person.name)")
        }
    )
    .store(in: &subscriptions)

GitHub

link
Stars: 4
Last commit: 5 days 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.

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