Home

Awesome

SwiftRequest

SwiftRequest is a lightweight, type-safe HTTP client for Swift, streamlining the construction and execution of HTTP requests.

OverviewInstallationSupported HTTP MethodsParametersLicense

Overview

SwiftRequest abstracts away the repetitive boilerplate code that's typically associated with setting up HTTP requests in Swift. It utilizes macros introduced in Swift 5.9, which can be associated with specific declarations to enhance and extend their functionality.

Here's a quick look at how you'd define a service to fetch quotes:

@Service(resource: "quotes")
protocol QuoteService {
    @GET("random")
    func getRandomQuotes(@QueryParam limit: Int?) async throws -> [Quote]
    
    @GET("{id}")
    func getQuote(@Path by id: String) async throws -> Quote
}

To make a request using SwiftRequest, you just need to create an instance of the service and call the method you want to use:

let service = QuoteServiceImpl(baseURL: "https://api.quotable.io")
let quotes = try await service.getRandomQuotes(limit: 5)
let quote = try await service.getQuote(by: "69Ldsxcdm-")

Installation

Xcode

It requires Xcode 15 or later.

In Xcode, go to File > Add Package Dependency and paste the repository URL:

https://github.com/ailtonvivaz/swift-request.git

Swift Package Manager

In Package.swift:

dependencies: [
    .package(url: "https://github.com/ailtonvivaz/swift-request.git", from: "0.1.0")
]

And then add the dependency to your targets.

Supported HTTP Methods

SwiftRequest offers support for the following HTTP methods:

Each of these methods accepts a string for the request path (optional). You can use just @GET or @GET("path") if you want to specify the path.

Parameters

SwiftRequest provides several parameters that can be used in conjunction with the HTTP methods:

License

SwiftRequest is available under the MIT license. See the LICENSE for details.