Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and docs #24

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "SendGridKit", targets: ["SendGridKit"])
],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.23.0")
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.24.0")
],
targets: [
.target(
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,34 @@ and add it to your target's dependencies:

Register the config and the provider.

~~~~swift
```swift
let httpClient = HTTPClient(...)
let sendGridClient = SendGridClient(httpClient: httpClient, apiKey: "YOUR_API_KEY")
~~~~
```

### Using the API

You can use all of the available parameters here to build your `SendGridEmail`.

Usage in a route closure would be as followed:

~~~~swift
```swift
import SendGridKit

let email = SendGridEmail(...)
try await sendGridClient.send(email)
~~~~
try await sendGridClient.send(email: email)
```

### Error handling

If the request to the API failed for any reason a `SendGridError` is thrown, which has an `errors` property that contains an array of errors returned by the API.

Simply ensure you catch errors thrown like any other throwing function.

~~~~swift
```swift
do {
try await sendGridClient.send(...)
try await sendGridClient.send(email: email)
} catch let error as SendGridError {
print(error)
}
~~~~
```
8 changes: 4 additions & 4 deletions Sources/SendGridKit/SendGridClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public struct SendGridClient: Sendable {

public func send<DynamicTemplateData: Codable & Sendable>(email: SendGridEmail<DynamicTemplateData>) async throws {
var headers = HTTPHeaders()
headers.add(name: "Authorization", value: "Bearer \(apiKey)")
headers.add(name: "Authorization", value: "Bearer \(self.apiKey)")
headers.add(name: "Content-Type", value: "application/json")

var request = HTTPClientRequest(url: apiURL)
var request = HTTPClientRequest(url: self.apiURL)
request.method = .POST
request.headers = headers
request.body = try HTTPClientRequest.Body.bytes(encoder.encode(email))
request.body = try HTTPClientRequest.Body.bytes(self.encoder.encode(email))

let response = try await httpClient.execute(request, timeout: .seconds(30))
let response = try await self.httpClient.execute(request, timeout: .seconds(30))

// If the request was accepted, simply return
if (200...299).contains(response.status.code) { return }
Expand Down
4 changes: 2 additions & 2 deletions Sources/SendGridKit/SendGridKit.docc/SendGridKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Usage in a route closure would be as followed:
import SendGridKit

let email = SendGridEmail(...)
try await sendGridClient.send(email)
try await sendGridClient.send(email: email)
```

### Error handling
Expand All @@ -32,7 +32,7 @@ Simply ensure you catch errors thrown like any other throwing function.

```swift
do {
try await sendGridClient.send(...)
try await sendGridClient.send(email: email)
} catch let error as SendGridError {
print(error)
}
Expand Down
Loading