-
Notifications
You must be signed in to change notification settings - Fork 10
an enum for the API Endpoints #744
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
Open
krugerk
wants to merge
22
commits into
beeminder:master
Choose a base branch
from
krugerk:feature/endpoint-enum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
bb6bf9f
endpoint enum
krugerk b133e2c
endpoint enum, most PUT
krugerk 1759098
datapoint
krugerk 51bd070
endpoint enum, migrated addDatapoint
krugerk b6034b9
endpoint enum, create/delete datapoint
krugerk 32bfda0
endpoint enum, register device token
krugerk fa63fd8
just one request manager
krugerk bd7d4a8
and tests back to compiling
krugerk 661e3cb
using url now that we have it
krugerk 72f7130
clean code
krugerk a6945b3
file for Endpoint
krugerk f507a2d
file for ServerError
krugerk 8559095
sample config was out of date
krugerk 8c70b40
bundle exec fastlane ios format
krugerk 20350f2
Endpoint
krugerk 44101ad
debug logging
krugerk 5b530c2
and now the tests work with the enum endpoint
krugerk d819fa3
deeplinkgen clean code
krugerk da0a09d
clean code
krugerk 2b66593
DeeplinkGenerator clean code
krugerk e037c8e
refactoring away from force unwrap
krugerk fa3d178
swift-format --recursive --in-place .
krugerk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,9 +12,9 @@ import SwiftyJSON | |
| // prevents effectively no-op updates due to float rounding | ||
| private let datapointValueEpsilon = 0.00000001 | ||
|
|
||
| let requestManager: RequestManager | ||
| let requestManager: RequestManaging | ||
|
|
||
| init(requestManager: RequestManager, container: BeeminderPersistentContainer) { | ||
| init(requestManager: RequestManaging, container: BeeminderPersistentContainer) { | ||
| self.requestManager = requestManager | ||
| self.modelContainer = container | ||
| let context = container.newBackgroundContext() | ||
|
|
@@ -30,28 +30,38 @@ import SwiftyJSON | |
| { | ||
| let val = datapoint.value | ||
| if datapointValue == val && comment == datapoint.comment { return } | ||
| let params = ["value": "\(datapointValue)", "comment": comment] | ||
| let _ = try await requestManager.put( | ||
| url: "api/v1/users/{username}/goals/\(goal.slug)/datapoints/\(datapoint.id).json", | ||
| parameters: params | ||
|
|
||
| let _ = try await requestManager.request( | ||
|
Contributor
Author
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. _ = ... Can also be used throughout, without the
Contributor
Author
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. would include SwiftLint for checking this |
||
| endpoint: .updateDatapoint( | ||
| username: goal.owner.username, | ||
| goalname: goal.slug, | ||
| datapointID: datapoint.id, | ||
| value: datapointValue, | ||
| comment: comment | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private func deleteDatapoint(goal: Goal, datapoint: DataPoint) async throws { | ||
| let _ = try await requestManager.delete( | ||
| url: "api/v1/users/{username}/goals/\(goal.slug)/datapoints/\(datapoint.id)" | ||
| let _ = try await requestManager.request( | ||
| endpoint: .deletedDatapoint(username: goal.owner.username, goalname: goal.slug, datapointID: datapoint.id) | ||
| ) | ||
| } | ||
|
|
||
| private func postDatapoint(goal: Goal, urText: String, requestId: String) async throws { | ||
| let _ = try await requestManager.addDatapoint(urtext: urText, slug: goal.slug, requestId: requestId) | ||
| let _ = try await requestManager.request( | ||
| endpoint: .createDatapoint( | ||
| username: goal.owner.username, | ||
| goalname: goal.slug, | ||
| urtext: urText, | ||
| requestID: requestId | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private func fetchDatapoints(goal: Goal, sort: String, per: Int, page: Int) async throws -> [DataPoint] { | ||
| let params = ["sort": sort, "per": per, "page": page] as [String: Any] | ||
| let response = try await requestManager.get( | ||
| url: "api/v1/users/{username}/goals/\(goal.slug)/datapoints.json", | ||
| parameters: params | ||
| let response = try await requestManager.request( | ||
| endpoint: .getDatapoints(username: goal.owner.username, goalname: goal.slug, sort: sort, page: page, per: per) | ||
| ) | ||
| let responseJSON = JSON(response!) | ||
|
|
||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| // Part of BeeSwift. Copyright Beeminder | ||
|
|
||
| import Alamofire | ||
| import Foundation | ||
| import OSLog | ||
| import SwiftyJSON | ||
|
|
||
| public enum Endpoint { | ||
| // signing in | ||
| case signIn(username: String, password: String, beemiosSecret: String) | ||
| // about the app versions the server expects to see | ||
| case appVersions | ||
| // Retrieves information and a list of goalnames for the user with username. | ||
| case getUser(username: String, diff_since: TimeInterval? = nil, emaciated: Bool? = nil) | ||
| // Gets goal details for user u's goal g | ||
| case getGoalDetails(username: String, goalname: String, datapoints_count: Int? = nil, emaciated: Bool? = nil) | ||
| // Get the list of datapoints for user u's goal g | ||
| case getDatapoints( | ||
| username: String, | ||
| goalname: String, | ||
| sort: String? = nil, | ||
| count: Int? = nil, | ||
| page: Int? = nil, | ||
| per: Int? = nil | ||
| ) | ||
|
|
||
| // Get all goals for a user | ||
| case getGoals(username: String, emaciated: Bool? = nil) | ||
| // Force a fetch of autodata and graph refresh | ||
| case requestAutodataFetch(username: String, goalname: String) | ||
| // Update the datapoint with ID id for user u's goal g (beeminder.com/u/g). | ||
| case updateDatapoint( | ||
| username: String, | ||
| goalname: String, | ||
| datapointID: String, | ||
| timestamp: Double? = nil, | ||
| value: NSNumber? = nil, | ||
| comment: String? = nil, | ||
| urtext: String? = nil | ||
| ) | ||
| // Update a goal for a user | ||
| case updateGoal( | ||
| username: String, | ||
| goalname: String, | ||
| title: String? = nil, | ||
| tmin: String? = nil, | ||
| tmax: String? = nil, | ||
| isSecret: Bool? = nil, | ||
| isDataPublic: Bool? = nil, | ||
| tags: [String]? = nil, | ||
| iiParams: [String: Any?]? = nil, | ||
| leadtime: Int? = nil, | ||
| alertstart: Int? = nil, | ||
| deadline: Int? = nil, | ||
| usesDefaultNotifications: Bool? = nil | ||
| ) | ||
| // Update the user | ||
| case updateUser( | ||
| username: String, | ||
| default_alertstart: Int? = nil, | ||
| default_deadline: Int? = nil, | ||
| default_leadtime: Int? = nil | ||
| ) | ||
| // Add a new datapoint to user u's goal g — beeminder.com/u/g. | ||
| case createDatapoint( | ||
| username: String, | ||
| goalname: String, | ||
| value: NSNumber? = nil, | ||
| timestamp: Double? = nil, | ||
| daystamp: String? = nil, | ||
| comment: String? = nil, | ||
| urtext: String? = nil, | ||
| requestID: String? = nil | ||
| ) | ||
| case deletedDatapoint(username: String, goalname: String, datapointID: String) | ||
| case registerDeviceToken(token: String, environment: String? = nil) | ||
|
|
||
| var url: URL { | ||
| var urlComponents: URLComponents { | ||
| var urlComponents = URLComponents() | ||
| urlComponents.scheme = "https" | ||
| urlComponents.host = Config().apiHost | ||
| urlComponents.path = self.path | ||
| return urlComponents | ||
| } | ||
| return urlComponents.url! | ||
| } | ||
| var path: String { | ||
| return switch self { | ||
| case .signIn: "/api/private/sign_in" | ||
|
|
||
| case .appVersions: "/api/private/app_versions.json" | ||
| case .registerDeviceToken: "/api/private/device_tokens" | ||
|
|
||
| case .getUser(let username, _, _), .updateUser(let username, _, _, _): "/api/v1/users/\(username).json" | ||
|
|
||
| case .getGoalDetails(let username, let goalname, _, _): "/api/v1/users/\(username)/goals/\(goalname)" | ||
| case .getDatapoints(let username, let goalname, _, _, _, _): | ||
| "/api/v1/users/\(username)/goals/\(goalname)/datapoints.json" | ||
| case .getGoals(let username, _): "/api/v1/users/\(username)/goals.json" | ||
| case .requestAutodataFetch(let username, let goalname): | ||
| "/api/v1/users/\(username)/goals/\(goalname)/refresh_graph.json" | ||
| case .updateDatapoint(let username, let goalname, let datapointID, _, _, _, _): | ||
| "/api/v1/users/\(username)/goals/\(goalname)/datapoints/\(datapointID).json" | ||
| case .updateGoal(let username, let goalname, _, _, _, _, _, _, _, _, _, _, _): | ||
| "/api/v1/users/\(username)/goals/\(goalname).json" | ||
| case .createDatapoint(let username, let goalname, _, _, _, _, _, _): | ||
| "/api/v1/users/\(username)/goals/\(goalname)/datapoints.json" | ||
| case .deletedDatapoint(let username, let goalname, let datapointID): | ||
| "/api/v1/users/\(username)/goals/\(goalname)/datapoints/\(datapointID).json" | ||
| } | ||
| } | ||
| var method: HTTPMethod { | ||
| return switch self { | ||
| case .signIn, .createDatapoint, .registerDeviceToken: .post | ||
| case .appVersions, .getUser, .getGoalDetails, .getDatapoints, .getGoals, .requestAutodataFetch: .get | ||
| case .updateDatapoint, .updateGoal, .updateUser: .put | ||
| case .deletedDatapoint: .delete | ||
| } | ||
| } | ||
| var parameters: [String: Any]? { | ||
| switch self { | ||
| case .signIn(let username, let password, let beemiosSecret): | ||
| return ["user": ["login": username, "password": password], "beemios_secret": beemiosSecret] as [String: Any] | ||
| case .getUser(_, let diff_since, let emaciated): | ||
| var parameters: [String: Any] = [:] | ||
| if let diff_since { parameters["diff_since"] = diff_since } | ||
| if let emaciated { parameters["emaciated"] = emaciated } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .getGoalDetails(_, _, let datapoints_count, let emaciated): | ||
| var parameters: [String: Any] = [:] | ||
| if let datapoints_count { parameters["datapoints_count"] = datapoints_count } | ||
| if let emaciated { parameters["emaciated"] = emaciated } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .getDatapoints(_, _, let sort, let count, let page, let per): | ||
| var parameters: [String: Any] = [:] | ||
| if let sort { parameters["sort"] = sort } | ||
| if let count { parameters["count"] = count } | ||
| if let page { parameters["page"] = page } | ||
| if let per { parameters["per"] = per } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .getGoals(_, let emaciated): | ||
| if let emaciated { return ["emaciated": emaciated] } | ||
| return nil | ||
| case .updateDatapoint(_, _, _, let timestamp, let value, let comment, let urtext): | ||
| var parameters: [String: Any] = [:] | ||
| if let timestamp { parameters["timestamp"] = timestamp } | ||
| if let value { parameters["value"] = value } | ||
| if let comment { parameters["comment"] = comment } | ||
| if let urtext { parameters["urtext"] = urtext } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .updateGoal( | ||
| _, | ||
| _, | ||
| let title, | ||
| let tmin, | ||
| let tmax, | ||
| let isSecret, | ||
| let isDataPublic, | ||
| let tags, | ||
| let iiParams, | ||
| let leadtime, | ||
| let alertstart, | ||
| let deadline, | ||
| let usesDefaultNotifications | ||
| ): | ||
| var parameters: [String: Any] = [:] | ||
| if let title { parameters["title"] = title } | ||
| if let tmin { parameters["tmin"] = tmin } | ||
| if let tmax { parameters["tmax"] = tmax } | ||
| if let isSecret { parameters["is_secret"] = isSecret } | ||
| if let isDataPublic { parameters["is_data_public"] = isDataPublic } | ||
| if let tags { parameters["tags"] = tags } | ||
| if let iiParams { parameters["ii_params"] = iiParams } | ||
| if let leadtime { parameters["leadtime"] = leadtime } | ||
| if let alertstart { parameters["alertstart"] = alertstart } | ||
| if let deadline { parameters["deadline"] = deadline } | ||
| if let usesDefaultNotifications { parameters["use_defaults"] = usesDefaultNotifications } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .updateUser(_, let default_alertstart, let default_deadline, let default_leadtime): | ||
| var parameters: [String: Any] = [:] | ||
| if let default_alertstart { parameters["default_alertstart"] = default_alertstart } | ||
| if let default_deadline { parameters["default_deadline"] = default_deadline } | ||
| if let default_leadtime { parameters["default_leadtime"] = default_leadtime } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .createDatapoint(_, _, let value, let timestamp, let daystamp, let comment, let urtext, let requestID): | ||
| var parameters: [String: Any] = [:] | ||
| if let value { parameters["value"] = value } | ||
| if let timestamp { parameters["timestamp"] = timestamp } | ||
| if let daystamp { parameters["daystamp"] = daystamp } | ||
| if let comment { parameters["comment"] = comment } | ||
| if let urtext { parameters["urtext"] = urtext } | ||
| if let requestID { parameters["request_id"] = requestID } | ||
| return parameters.isEmpty ? nil : parameters | ||
| case .registerDeviceToken(let token, let environment): | ||
| var parameters: [String: Any] = [:] | ||
| parameters["device_token"] = token | ||
| if let environment { parameters["server"] = environment } | ||
| return parameters.isEmpty ? nil : parameters | ||
| default: return nil | ||
| } | ||
| } | ||
| var shouldSign: Bool { | ||
| return switch self { | ||
| case .registerDeviceToken: true | ||
| default: false | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.