kperson / swift-async-http

An Async Http Client for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift Async Http

Swift Async Http is a basic async HTTP client that wraps URLSession and URLRequest. This client works with iOS, tvOS, macOS, watchOS, and Linux.

master: CircleCI release: CircleCI dev: CircleCI

Cocoapod Installation

Add the following to your Podfile.

pod 'AsyncHttp'

Swift Package Installation

Add the following to your Package.

Package Dependency

.package(url: "https://github.com/kperson/swift-async-http.git", .upToNextMajor(from: "1.1.1"))

Target Dependency

.product(name: "AsyncHttp", package: "swift-async-http")

Basic Usuage

import AsyncHttp

let client = HttpClient()
// you may specify session (URLSession), cachePolicy, and timeoutInterval optionaly for the client

let request = Request(
    method : .POST,
    url: "https://www.example.com/my-path",
    headers: ["k" : "v"]
    body: Data()
)
// you can override session (URLSession), cachePolicy, and timeoutInterval for reach request if you like by providing extra parameters

let response = try await client.fetch(request)
print(response.statusCode)
print(response.headers["some_header"]) //NOTE: headers are case insenstive
print(response.headers["some_HEADER"]) //NOTE: headers are case insenstive
//the above two lines are equivalent

Request Builder

import AsyncHttp

let builder = RequestBuilder(
    method: .GET, 
    url: "https://www.example.com/my-path"
)
builder.addHeader(field: "k", value: "v")
builder.addQueryParam(nane: "q", value: "hello")
// you may also specify body, session (URLSession), cachePolicy, and timeoutInterval
let request = builder.build()
let response = try await client.fetch(request)
print(response.statusCode)

About

An Async Http Client for Swift

License:MIT License


Languages

Language:Swift 97.4%Language:Ruby 2.4%Language:Dockerfile 0.2%