Skip to content

Commit

Permalink
Section Using, added Vapor sample in README.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoMachorro committed Jul 23, 2023
1 parent e200626 commit 3f8ad9a
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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*.
Expand Down

0 comments on commit 3f8ad9a

Please sign in to comment.