-
-
Notifications
You must be signed in to change notification settings - Fork 5
Adds About and Social API #160
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import Vapor | ||
|
|
||
| struct AboutAPIController: RouteCollection { | ||
| func boot(routes: RoutesBuilder) throws { | ||
| routes.get(use: onGet) | ||
| } | ||
|
|
||
| @Sendable func onGet(request: Request) async throws -> Response { | ||
| let aboutData = AboutResponse( | ||
| title: "About", | ||
| description: [ | ||
| "Founded by Adam Rush in 2019, SwiftLeeds has set out to bring a modern, inclusive conference to the north of the UK.", | ||
| "Ran by just a handful of community volunteers, SwiftLeeds is entirely non-profit with every penny going into delivering the best experience possible.", | ||
| "In-person conferences are the best way to meet like-minded people who enjoy building apps with Swift. You can also learn from the best people in the industry and chat about all things Swift." | ||
| ], | ||
| foundedYear: "2019", | ||
| founderName: "Adam Rush", | ||
| founderTwitter: "https://twitter.com/Adam9Rush" | ||
| ) | ||
|
|
||
| let response = GenericResponse( | ||
| data: aboutData | ||
| ) | ||
|
|
||
| return try await response.encodeResponse(for: request) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import Foundation | ||
| import Vapor | ||
|
|
||
| struct AboutResponse: Content { | ||
| let title: String | ||
| let description: [String] | ||
| let foundedYear: String | ||
| let founderName: String | ||
| let founderTwitter: String | ||
|
Comment on lines
+7
to
+9
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of extracting these values? How can we leverage the new team API to prevent duplicating data? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,10 @@ struct HomeRouteController: RouteCollection { | |
|
|
||
| let phase = try getPhase(req: req, event: event) | ||
|
|
||
| // Fetch About and Social data from API endpoints to maintain single source of truth | ||
| let aboutData: AboutResponse? = try await fetchAboutData(req: req) | ||
| let socialData: SocialResponse? = try await fetchSocialData(req: req) | ||
|
Comment on lines
+97
to
+98
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're adding a lot of complexity to the views to handle the fact these are optional. Let's make them required, which will be easier when we address the other PR comment, and then simplify the UI to always use it. |
||
|
|
||
| let schedule = event.days | ||
| .map { day in | ||
| ScheduleDay( | ||
|
|
@@ -118,7 +122,9 @@ struct HomeRouteController: RouteCollection { | |
| dropInSessions: dropInSessions, | ||
| schedule: phase.showSchedule ? schedule : [], | ||
| phase: PhaseContext(phase: phase, event: event), | ||
| event: eventContext | ||
| event: eventContext, | ||
| about: aboutData, | ||
| social: socialData | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -188,6 +194,38 @@ struct HomeRouteController: RouteCollection { | |
|
|
||
| return try await Event.getCurrent(on: req.db) | ||
| } | ||
|
|
||
| private func fetchAboutData(req: Request) async throws -> AboutResponse? { | ||
| do { | ||
| // Make internal request to the About API endpoint | ||
| let aboutRequest = Request(application: req.application, method: .GET, url: URI(string: "/api/v2/about"), on: req.eventLoop) | ||
| let aboutController = AboutAPIController() | ||
| let aboutResponse = try await aboutController.onGet(request: aboutRequest) | ||
|
|
||
| guard let data = aboutResponse.body.data else { return nil } | ||
| let genericResponse = try JSONDecoder().decode(GenericResponse<AboutResponse>.self, from: data) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I gave the teams API #159 a free pass because it wasn't decoding JSON, but we absolutely should not be calling our own internal APIs through the service and then parsing JSON responses - it's extremely wasteful on bandwidth, memory, and compute cycles. I'd recommend pulling the data out into a more accessible location first, and accessing it from the API and HomeRouteController. |
||
| return genericResponse.data | ||
| } catch { | ||
| req.logger.warning("Failed to fetch about data from API: \(error)") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| private func fetchSocialData(req: Request) async throws -> SocialResponse? { | ||
| do { | ||
| // Make internal request to the Social API endpoint | ||
| let socialRequest = Request(application: req.application, method: .GET, url: URI(string: "/api/v2/social"), on: req.eventLoop) | ||
| let socialController = SocialAPIController() | ||
| let socialResponse = try await socialController.onGet(request: socialRequest) | ||
|
|
||
| guard let data = socialResponse.body.data else { return nil } | ||
| let genericResponse = try JSONDecoder().decode(GenericResponse<SocialResponse>.self, from: data) | ||
| return genericResponse.data | ||
| } catch { | ||
| req.logger.warning("Failed to fetch social data from API: \(error)") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct Phase { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import Vapor | ||
|
|
||
| struct SocialAPIController: RouteCollection { | ||
| func boot(routes: RoutesBuilder) throws { | ||
| routes.get(use: onGet) | ||
| } | ||
|
|
||
| @Sendable func onGet(request: Request) async throws -> Response { | ||
| let socialData = SocialResponse(socialLinks: [ | ||
| SocialLink(id: "twitter", name: "Twitter", url: "https://twitter.com/swift_leeds", icon: "bx bxl-twitter", displayName: "@Swift_Leeds", order: 1), | ||
| SocialLink(id: "mastodon", name: "Mastodon", url: "https://iosdev.space/@swiftleeds", icon: "bx bxl-mastodon", displayName: "@swiftleeds", order: 2), | ||
| SocialLink(id: "youtube", name: "YouTube", url: "https://www.youtube.com/channel/UCCq1K0eWKZFBCpqaC3n8V1g", icon: "bx bxl-youtube", displayName: nil, order: 3), | ||
| SocialLink(id: "slack", name: "Join Slack", url: "https://join.slack.com/t/swiftleedsworkspace/shared_invite/zt-wkmr6pif-ZDCdDeHM60jcBUy0BxHdCQ", icon: "bx bxl-slack", displayName: nil, order: 4), | ||
| SocialLink(id: "flickr", name: "Flickr", url: "https://www.flickr.com/photos/196979204@N02/albums/72177720303878744", icon: "bx bxl-flickr", displayName: nil, order: 5), | ||
| SocialLink(id: "spotify", name: "Spotify", url: "https://open.spotify.com/show/3pHsjVt54MDDHdzZce7ezl", icon: "bx bxl-spotify", displayName: nil, order: 6) | ||
| ]) | ||
|
|
||
| let response = GenericResponse( | ||
| data: socialData | ||
| ) | ||
|
|
||
| return try await response.encodeResponse(for: request) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import Foundation | ||
| import Vapor | ||
|
|
||
| struct SocialResponse: Content { | ||
| let socialLinks: [SocialLink] | ||
| } | ||
|
|
||
| struct SocialLink: Content { | ||
| let id: String | ||
| let name: String | ||
| let url: String | ||
| let icon: String | ||
| let displayName: String? | ||
| let order: Int | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed, we can add Spotify. There was no reason for it to be header only aside from I missed it.