Skip to content

Commit

Permalink
Update dependencies and docs (#24)
Browse files Browse the repository at this point in the history
* Update dependencies

* Fix docs typos

* Make internal code clearer
  • Loading branch information
fpseverino authored Dec 15, 2024
1 parent cd3d999 commit e165ef0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
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

0 comments on commit e165ef0

Please sign in to comment.