-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section Using, added Vapor sample in README.
- Loading branch information
1 parent
e200626
commit 3f8ad9a
Showing
1 changed file
with
29 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,18 @@ Reach out in [Discussions](https://github.com/RobertoMachorro/SwiftGravatar/disc | |
|
||
# Using | ||
|
||
## Setup | ||
|
||
Add the SwiftGravatar package to your project or via Package.swift using the address: | ||
|
||
https://github.com/RobertoMachorro/SwiftGravatar | ||
|
||
<img width="832" alt="image" src="https://github.com/RobertoMachorro/SwiftGravatar/assets/7190436/ba170546-30f6-482b-8c0d-eb4d055e8eaa"> | ||
|
||
Feel free to peek into the code to see the whole Model as well as check the tests for usage options. | ||
|
||
## Async/Await | ||
|
||
Fetching the profile from the server can be done many ways, the easiest is to use the convenience function *GravatarProfile.getProfile*: | ||
|
||
```swift | ||
|
@@ -44,6 +50,27 @@ let (data, _) = try await URLSession.shared.data(from: url) | |
let profile = try JSONDecoder().decode(GravatarProfile.self, from: data) | ||
``` | ||
|
||
## Vapor / SwiftNIO | ||
|
||
The request client can be leveraged to make a call to Gravatar: | ||
|
||
```swift | ||
func get(using email: String, on request: Request) -> EventLoopFuture<GravatarProfile> { | ||
guard let gravatarAddy = GravatarProfile.getProfileAddress(using: email) else { | ||
return request.eventLoop.makeFailedFuture(Abort(.badRequest)) | ||
} | ||
return request.client.get(URI(string: gravatarAddy)) | ||
.flatMapThrowing { response in | ||
guard response.status == .ok else { | ||
return GravatarProfile(entry: []) | ||
} | ||
return try response.content.decode(GravatarProfile.self) | ||
} | ||
} | ||
``` | ||
|
||
## Model / Decoding | ||
|
||
The data will be stored in the GravatarProfile model: | ||
|
||
```swift | ||
|
@@ -72,15 +99,15 @@ public struct GravatarProfile: Decodable { | |
} | ||
``` | ||
|
||
## Just the URL, thanks | ||
|
||
An easy converter from e-mail to Gravatar URL can be accessed as follows: | ||
|
||
```swift | ||
let myemailaddress = GravatarProfile.getProfileAddress(using: "[email protected]") | ||
// "https://en.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346.json" | ||
``` | ||
|
||
Feel free to peek into the code to see the whole Model as well as check the tests for usage options. | ||
|
||
# Contributing | ||
|
||
Contributions are very welcome. Fork the repo, make your changes, test with SwiftLint and Unit tests, commit and do a *pull request*. | ||
|