CoreNetwork module with the basic functionality of requests to the network

Overview

CoreNetwork

CoreNetwork module with the basic functionality of requests to the network.

Integration

if as local package

    dependencies: [
        .package(path: "../CoreNetwork")
    ],

if as shared package

    dependencies: [
        .package(name: "CoreNetwork", url: "https://github.com/MosMetro-official/CoreNetwork.git", from: "0.0.1")
    ],

Structure

The Package is a wrapper for easy network requests, that is includes SwiftyJSON lib inside. Available for iOS 13+, as it uses the async/await structure.

Usage

First of all, it is necessary to designate the main host for the request - for different hosts, you can use different static variables, for convenience.

import CoreNetwork

extension APIClient {
    
    public static var myClient : APIClient {
        return APIClient(host: "YOUR_AWESOME_HOST.com")
    }
}

It's also possible at this stage to make a different host for several project profiles, when using, for example, SettingsBundle. It should be noted that at this stage it is not necessary to attribute the protocol of using HTTP - this is done at the request stage, and HTTPS is always used by default.

Due to the fact that SwiftyJSON is already integrated, it does not need to be additionally connected to the project - we are closely monitoring its updates and updating our wrapper =)

func loadSomeStuff() async throws -> VALUE {
    let client = APIClient.myClient
    do {
        let responce = try await client.send(
            .GET(
                path: "/MY_AWESOME_PATH"
            )
        )
        let json = JSON(responce.data)
        let myValue : VALUE = json["VALUE_KEY"]
        return myValue
    } catch {
        throw error
    }
}

NOTE: Path must necessarily start with "/" -> it will throws a "badURL" error.

Delegate

You can subscribe to APIClientDelegate to track some events, 2 methods are implemented now:

public protocol APIClientDelegate {
    
    func client(_ client: APIClient, willSendRequest request: inout URLRequest)
    
    func client(_ client: APIClient, initialRequest: Request, didReceiveInvalidResponse response: HTTPURLResponse, data: Data?) async throws -> Response
}

'willSendRequest' tracks when a request is about to be sent - it is useful to use, for example, for a metric or a load indicator (don't forget about streams). In default implementation does nothing. 'didReceiveInvalidResponse' did Receive Invalid Response works out when the server gives an invalid response, by default throws an error with an error code.

Errors

In addition to the errors from SwiftyJSON, we also added 5 errors that can occur when working with the network:

    case badURL
    case badData
    case badRequest
    case noHTTPResponse
    case unacceptableStatusCode(Int)

The 'errorDescription' is not localized yet, so you can override them - it's OK)

public var errorDescription : String {
    switch self {
    case .badURL:
        return "😣😣😣 URL is bad."
        
    case .badData:
        return "😣😣😣 The data we received is bad."
        
    case .badRequest:
        return "😣😣😣 Couldn't send a request."
        
    case .noHTTPResponse:
        return "😣😣😣 The server didn't send anything."
        
    case .unacceptableStatusCode(let statusCode):
        return "😣😣😣 Response status code was unacceptable: \(statusCode)."
    }
}
You might also like...
Synchronous requests for AFNetworking 1.x, 2.x, and 3.x
Synchronous requests for AFNetworking 1.x, 2.x, and 3.x

AFNetworking-Synchronous A minimal category which extends AFNetworking to support synchronous requests. Usage 3.x pod 'AFNetworking', '~ 3.0' pod

Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman)

RequestAdapter Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman) The code is taken from:

Quickstart-ios-swift-grpc - Quickstart for integrating Approov with iOS apps in Swift that make API requests you wish to protect using GRPC A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning.
A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning.

FridaHookSwiftAlamofire A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning. δΈ­ζ–‡ζ–‡ζ‘£εŠθΏ‡η¨‹ Features Ca

A very basic proof-of-concept Swift HTTP server that does not require Foundation

Swift Server Introduction This is very rough and basic HTTP server written in Swift without using Foundation. This is partially based on the Swifter r

Dratini is a neat network abstraction layer.

Dratini Dratini is a neat network abstraction layer. If you are looking for a solution to make your network layer neat, Dratini is your choice. Dratin

Network abstraction layer written in Swift.
Network abstraction layer written in Swift.

Moya 14.0.0 A Chinese version of this document can be found here. You're a smart developer. You probably use Alamofire to abstract away access to URLS

A toolkit for Network Extension Framework

NEKit NEKit is deprecated. It should still work but I'm not intent on maintaining it anymore. It has many flaws and needs a revamp to be a high-qualit

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
A lightweight, one line setup, iOS / OSX network debugging library! 🦊

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Releases(0.0.3-callbacks)
Owner
Moscow Metro
Moscow Metro
Menet is a TCP network middleware that can be dynamically modified through HTTP requests.

Menet Menet is a TCP network middleware that can be dynamically modified through HTTP requests. This is an experimental project, do NOT use it in prod

Nik 2 May 19, 2022
VFNetwork is a protocol-oriented network layer that will help you assemble your requests in just a few steps.

Simple, Fast and Easy. Introduction VFNetwork is a protocol-oriented network layer that will help you assemble your requests in just a few steps. How

Victor Freitas 4 Aug 22, 2022
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Ronan Rodrigo Nunes 89 Dec 26, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
Say goodbye to the Fat ugly singleton Network Manager with this Network Layer

MHNetwork Protocol Oriented Network Layer Aim to avoid having bloated singleton NetworkManager Philosophy the main philosophy behind MHNetwork is to h

Mohamed Emad Hegab 19 Nov 19, 2022
NWReachability - a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

NWReachability is a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

null 4 Dec 2, 2022
🌸 Powerful Codable API requests builder and manager for iOS.

This lib is about network requests with blackjack, roulette and craps! Using it you will be able to convert your massive API layer code into an awesom

CodyFire 251 Jan 8, 2023
Thin wrapper around NSURLSession in swift. Simplifies HTTP requests.

SwiftHTTP SwiftHTTP is a thin wrapper around NSURLSession in Swift to simplify HTTP requests. Features Convenient Closure APIs Simple Queue Support Pa

Dalton 1.9k Dec 7, 2022
Easy to use CFNetwork wrapper for HTTP requests, Objective-C, Mac OS X and iPhone

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier

Ben Copsey 5.8k Dec 14, 2022
a NSURLCache subclass for handling all web requests that use NSURLRequest

EVURLCache What is this? This is a NSURLCache subclass for handeling all web requests that use NSURLRequest. (This includes UIWebView) The EVURLCache

Edwin Vermeer 296 Dec 18, 2022