Update README

This commit is contained in:
Ailton Vieira Pinto Filho
2023-06-11 21:22:12 -03:00
committed by Ailton Vieira
parent 6c3e36fffe
commit 1c6d6ca08d
4 changed files with 79 additions and 40 deletions

View File

@@ -1,30 +1,36 @@
# SwiftRequest
A type-safe HTTP client for Swift.
SwiftRequest is a lightweight, type-safe HTTP client for Swift, streamlining the construction and execution of HTTP requests.
## Introduction
## Overview
SwiftRequest creates all the boilerplate code for you HTTP requests. It uses attached macros, a feature introduced in Swift 5.9, which are associated with a specific declaration in the program that they can augment and extend.
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:
```swift
@Service(route: "quotes")
@Service(resource: "quotes")
class QuoteService {
@GET<[Quote]>("random")
private func getRandomQuotes(@QueryParam limit: Int? = nil) {}
@GET<Quote>("{id}")
private func getQuote(@PathParam by id: String) {}
}
```
With this code, you can make a request to the API:
To make a request using SwiftRequest, you can do the following:
```swift
let baseURL = URL(string: "https://api.quotable.io")!
let service = QuoteService(baseURL: baseURL)
let (quotes, _) = try await service.getRandomQuotes()
let (quotes, _) = try await service.getRandomQuotes(limit: 5)
let (quote, _) = try await service.getQuote(by: "69Ldsxcdm")
```
## Available HTTP Methods
## Supported HTTP Methods
SwiftRequest supports the following HTTP methods:
SwiftRequest offers support for the following HTTP methods:
- `@GET`
- `@POST`
@@ -34,12 +40,45 @@ SwiftRequest supports the following HTTP methods:
- `@HEAD`
- `@OPTIONS`
Each of these methods accepts a generic response type (could be `Data`), a string for the request path, and an optional dictionary to specify request headers.
```swift
@GET<Quote>("{id}", headers: ["Cache-Control": "max-age=640000"])
```
## Parameters
SwiftRequest provides several parameters that can be used in conjunction with the HTTP methods:
- `@Header`: This property wrapper can be used to specify a request header.
- `@QueryParam`: This property wrapper can be used to specify a URL query parameter.
- `@PathParam`: This property wrapper can be used to specify a path parameter in the URL.
- `@Body`: This property wrapper can be used to specify the request body.
- `@FieldParam`: This property wrapper can be used to specify a field parameter in the request body.
- `@Header`: Use this property wrapper to define a request header. The header name is optional. If it's not provided, SwiftRequest uses the property name as the header name.
```swift
func getQuote(@Header("Cache-Control") cacheControl: String) {}
```
- `@QueryParam`: Use this property wrapper to define a URL query parameter. The query parameter name is optional. If it's not provided, SwiftRequest uses the property name as the query parameter name.
```swift
func getRandomQuotes(@QueryParam limit: Int? = nil) {}
```
In this case, the limit parameter will be used as the query parameter name. Example: `https://api.quotable.io/quotes/random?limit=10`
- `@PathParam`: Use this property wrapper to define a path parameter in the URL. The path parameter name is optional. If it's not provided, SwiftRequest uses the property name as the path parameter name.
```swift
@GET<Quote>("{id}")
func getQuote(@PathParam by id: String) {}
```
In this case, the id parameter will be used as the path parameter name. Example: `https://api.quotable.io/quotes/123`
> It's important to note that the path parameter name must match the name of the property that's being used to define the path parameter and need to be write in the path between curly braces.
- `@Body`: Use this property wrapper to define the request body. This wrapper can only be used with the `@POST`, `@PUT`, `@PATCH`, and `@DELETE` HTTP methods.
```swift
@POST<Quote>("quotes")
func createQuote(@Body quote: Quote) {}
```
Here, the quote parameter will be used as the request body, and the `Content-Type: application/json` header will be automatically added to the request.
- `@FieldParam`: Use this property wrapper to define a field parameter in the request body. This wrapper can only be used with the `@POST`, `@PUT`, `@PATCH`, and `@DELETE` HTTP methods.
```swift
@POST<Quote>("quotes")
func createQuote(@FieldParam("author") authorName: String, @FieldParam content: String) {}
```
In this case, the author and content parameters will be used as field parameters in the request body, and the `Content-Type: application/x-www-form-urlencoded` header will be automatically added to the request. Example: `author=John%20Doe&content=Hello%20World`
## License
SwiftRequest is available under the MIT license. See the [LICENSE](LICENSE) for details.

View File

@@ -0,0 +1,13 @@
struct Quote: Decodable {
let id: String
let content: String
let author: String
let tags: [String]
enum CodingKeys: String, CodingKey {
case id = "_id"
case content
case author
case tags
}
}

View File

@@ -0,0 +1,11 @@
import Foundation
import SwiftRequest
@Service(resource: "quotes")
class QuoteService {
@GET<[Quote]>("random")
private func getRandomQuotes(@QueryParam limit: Int? = nil) {}
@GET<Quote>("{id}")
private func getQuote(@PathParam by id: String) {}
}

View File

@@ -1,37 +1,13 @@
import SwiftRequest
import Foundation
struct Quote: Decodable {
let id: String
let content: String
let author: String
let tags: [String]
enum CodingKeys: String, CodingKey {
case id = "_id"
case content
case author
case tags
}
}
@Service(route: "quotes")
class QuoteService {
@GET<[Quote]>("random")
private func getRandomQuotes(@QueryParam("author") authorSlug: String? = nil) {}
@GET<Quote>("{id}")
private func getQuote(@PathParam by id: String) {}
}
let baseURL = URL(string: "https://api.quotable.io")!
let service = QuoteService(baseURL: baseURL)
do {
let (quotes, _) = try await service.getRandomQuotes(authorSlug: "agatha-christie")
print(quotes[0])
let (quotes, _) = try await service.getRandomQuotes(limit: 3)
print(quotes)
let (quote, _) = try await service.getQuote(by: quotes[0].id)
let (quote, _) = try await service.getQuote(by: "69Ldsxcdm")
print(quote)
} catch {
print(error)