From 3f8ad9aa3a330447d2f31c18faf066516a73c3f0 Mon Sep 17 00:00:00 2001 From: Roberto Machorro <7190436+RobertoMachorro@users.noreply.github.com> Date: Sat, 22 Jul 2023 20:11:18 -0400 Subject: [PATCH] Section Using, added Vapor sample in README. --- README.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a7ae188..d749d57 100644 --- a/README.md +++ b/README.md @@ -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 image +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 { + 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,6 +99,8 @@ public struct GravatarProfile: Decodable { } ``` +## Just the URL, thanks + An easy converter from e-mail to Gravatar URL can be accessed as follows: ```swift @@ -79,8 +108,6 @@ let myemailaddress = GravatarProfile.getProfileAddress(using: "myemailaddress@ex // "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*.