Coder Social home page Coder Social logo

networkrequester's Introduction

NetworkRequester

NetworkRequester is an HTTP Combine-only networking library.

Requirements

  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+
  • Xcode 12+
  • Swift 5.3+

Installation

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

NetworkRequester supports only SPM and adding it as a dependency is done just by including the URL into the dependencies value of your Package.swift.

dependencies: [
    .package(
        url: "https://github.com/igashev/NetworkRequester.git",
        .upToNextMajor(from: "1.2.0")
    )
]

Or by using the integrated tool of Xcode.

Usage

Building requests

NetworkRequester provides a very easy way to build a request using URLRequestBuilder.

// Creating a builder
let requestBuilder = URLRequestBuilder(
    environment: Environment.production,
    endpoint: UsersEndpoint.users,
    httpMethod: .get,
    httpHeaders: [
        .json, 
        .authorization(bearerToken: "secretBearerToken")
    ],
    httpBody: nil,
    queryParameters: nil
)

Calling requests

There are two options with which to make the actual network request.

The first option is using plain URLRequest.

struct User: Decodable {
    let name: String
}

struct BackendError: DecodableError {
    let errorCode: Int
    let localizedError: String
}

let url = URL(string: "https://amazingapi.com/v1/users")!
let urlRequest = URLRequest(url: url)

let caller = AsyncCaller(decoder: JSONDecoder())
let user: User = try await caller.call(
    using: urlRequest, 
    errorType: BackendError.self
)

The second option is using URLRequestBuilder.

struct User: Decodable {
    let name: String
}

struct BackendError: DecodableError {
    let errorCode: Int
    let localizedError: String
}

let requestBuilder = URLRequestBuilder(
    environment: "https://amazingapi.com",
    endpoint: "v1/users",
    httpMethod: .get,
    httpHeaders: [
        .json, 
        .authorization(bearerToken: "secretBearerToken")
    ],
    httpBody: nil,
    queryParameters: nil
)

let caller = AsyncCaller(decoder: JSONDecoder())
let user: User = try await caller.call(
    using: requestBuilder, 
    errorType: BackendError.self
)

Take into account that when a response data is expected, a type that conforms to Encodable should be specified as Output. Otherwise Void.

networkrequester's People

Contributors

igashev avatar

Watchers

 avatar

Forkers

kanevp

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.