Swiftpack.co - immobiliare/RealHTTP as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by immobiliare.
immobiliare/RealHTTP 1.4.0
🌀Web API client & Stubber built with async/await for Swift
⭐️ 154
🕓 2 weeks ago
iOS macOS linux macOS iOS
.package(url: "https://github.com/immobiliare/RealHTTP.git", from: "1.4.0")

RealHTTP

RealHTTP is a lightweight yet powerful async/await based client-side HTTP library made in Swift.
The goal of this project is to make an easy to use, effortless http client based upon all the best new Swift features.

What you will get?

This is a simple http call in RealHTTP

let todo = try await HTTPRequest("https://jsonplaceholder.typicode.com/todos/1")
                     .fetch(Todo.self)

One line of code, including the automatic decode from JSON to object.

Of course you can fully configure the request with many other parameters, take a look here:

let req = HTTPRequest {
    // Setup default params
    $0.url = URL(string: "https://.../login")!
    $0.method = .post
    $0.timeout = 15

    // Setup some additional settings
    $0.redirectMode = redirect
    $0.maxRetries = 4
    $0.headers = HTTPHeaders([
        .init(name: .userAgent, value: myAgent),
        .init(name: "X-API-Experimental", value: "true")
    ])
    
    // Setup URL query params & body
    $0.addQueryParameter(name: "full", value: "1")
    $0.addQueryParameter(name: "autosignout", value: "30")
    $0.body = .json(["username": username, "pwd": pwd])
}
let _ = try await req.fetch()

This is fully type-safe too!

What about the stubber?

Integrated stubber is perfect to write your own test suite:

That's a simple stubber which return the original request as response:

let echoStub = HTTPStubRequest().match(urlRegex: "*").stubEcho()
HTTPStubber.shared.add(stub: echoStub)
HTTPStubber.shared.enable()

Of course you can fully configure your stub with rules (regex, URI template and more):

// This is a custom stubber for any post request.
var stub = HTTPStubRequest()
           .stub(for: .post, { response in
                response.responseDelay = 5
                response.headers = HTTPHeaders([
                    .contentType: HTTPContentType.bmp.rawValue,
                    .contentLength: String(fileSize,
                ])
                response.body = fileContent
            })
HTTPStubber.shared.add(stub: stub)

That's all!

Feature Highlights

RealHTTP offers lots of features and customization you can found in our extensive documentation and test suite.
Some of them are:

  • Async/Await native support
  • Requests queue built in
  • Based upon native URLSession technology
  • Advanced retry mechanisms
  • Chainable & customizable response validators like Node's Express.js
  • Automatic Codable object encoding/decoding
  • Customizable decoding of objects

And for pro users:

  • Powerful integrated HTTP Stub for your mocks
  • Combine publisher adapter
  • URI templating system
  • Resumable download/uploads with progress tracking
  • Native Multipart Form Data support
  • Advanced URL connection metrics collector
  • SSL Pinning, Basic/Digest Auth
  • TSL Certificate & Public Key Pinning
  • cURL debugger

Documentation

RealHTTP is provided with an extensive documentation.

Test

RealHTTP has an extensive unit test suite which covers many of the standard and edge cases including request build, parameter encoding, queuing and retry strategies.
See the XCTest suite inside Tests/RealHTTPTests folder.

Requirements

RealHTTP can be installed in any platform which supports:

  • iOS 13+, macOS Catalin+, watchOS 6+, tvOS 13+
  • Xcode 13.2+
  • Swift 5.5+

Installation

Swift Package Manager

Aadd it as a dependency in a Swift Package, add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/immobiliare/RealHTTP.git", from: "1.0.0")
]

And add it as a dependency of your target:

targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "https://github.com/immobiliare/RealHTTP.git", package: "RealHTTP")
    ])
]

In Xcode 11+ you can also navigate to the File menu and choose Swift Packages -> Add Package Dependency..., then enter the repository URL and version details.

CocoaPods

RealHTTP can be installed with CocoaPods by adding pod 'RealHTTP' to your Podfile.

pod 'RealHTTP'

Powered Apps

RealHTTP was created by the amazing mobile team at ImmobiliareLabs, the Tech dept at Immobiliare.it, the first real estate site in Italy.
We are currently using RealHTTP in all of our products.

If you are using RealHTTP in your app drop us a message, we'll add below.

Indomio

Support & Contribute

Made with ❤️ by ImmobiliareLabs & Contributors

We'd love for you to contribute to RealHTTP!
If you have any questions on how to use RealHTTP, bugs and enhancement please feel free to reach out by opening a GitHub Issue.

GitHub

link
Stars: 154
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.4.0
2 weeks ago

2022-04-27

New Features / Enhancements

  • [NEW] #40 - Ability to return a custom HTTPResponse as result of a validation inside the HTTPValidator response using the nextValidatorWithResponse action response.

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