diff --git a/PagerCall/PagerDutyAPI.swift b/PagerCall/PagerDutyAPI.swift index f39896b..d1444d9 100644 --- a/PagerCall/PagerDutyAPI.swift +++ b/PagerCall/PagerDutyAPI.swift @@ -33,31 +33,12 @@ struct PagerDutyAPI { } } - func getUserID() async throws -> String { - if !userID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { - return userID - } - - let user = try await getUser() - - return user.id - } - - private func getUser() async throws -> UsersMeResp.User { - let data = try await get("/users/me") - let decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - let resp = try decoder.decode(UsersMeResp.self, from: data) - - return resp.user - } - private struct OncallsResp: Codable { let oncalls: [Oncall] struct Oncall: Codable {} } - func isOnCall(_ userID: String) async throws -> Bool { + func isOnCall() async throws -> Bool { let data = try await get("/oncalls", ["user_ids[]": userID]) let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase @@ -70,7 +51,7 @@ struct PagerDutyAPI { let incidents: Incidents } - func getIncidents(_ userID: String) async throws -> Incidents { + func getIncidents() async throws -> Incidents { let data = try await get("/incidents", ["user_ids[]": userID]) let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase @@ -79,26 +60,6 @@ struct PagerDutyAPI { return resp.incidents } - func getIncidentsURL() async throws -> URL { - let user = try await getUser() - let url = URL(string: user.htmlUrl)! - let uid = userID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? user.id : userID - var components = URLComponents(url: url, resolvingAgainstBaseURL: false)! - components.path = "/incidents" - components.query = "assignedToUser=\(uid)" - - return components.url! - } - - func getOnCallShiftsURL() async throws -> URL { - let user = try await getUser() - let url = URL(string: user.htmlUrl)! - var components = URLComponents(url: url, resolvingAgainstBaseURL: false)! - components.path = "/my-on-call/week" - - return components.url! - } - private func get(_ path: String, _ query: [String: String] = [:]) async throws -> Data { var url = endpoint.appendingPathComponent(path) url.append(queryItems: query.map { key, val in URLQueryItem(name: key, value: val) }) diff --git a/PagerCall/PagerDutyModel.swift b/PagerCall/PagerDutyModel.swift index 1621c04..a255de1 100644 --- a/PagerCall/PagerDutyModel.swift +++ b/PagerCall/PagerDutyModel.swift @@ -20,9 +20,8 @@ class PagerDuty: ObservableObject { func update() async { do { - let userID = try await api.getUserID() - let onCallNow = try await api.isOnCall(userID) - let currIncidents = try await api.getIncidents(userID) + let onCallNow = try await api.isOnCall() + let currIncidents = try await api.getIncidents() let newIncidents = currIncidents - incidents let hasIncidents = !currIncidents.isEmpty diff --git a/PagerCall/RightClickMenu.swift b/PagerCall/RightClickMenu.swift index 5395a47..df945ed 100644 --- a/PagerCall/RightClickMenu.swift +++ b/PagerCall/RightClickMenu.swift @@ -1,27 +1,18 @@ import SwiftUI struct RightClickMenu: View { + @AppStorage("subdomain") private var subdomain = "" private let api = PagerDutyAPI() var body: some View { Button("My Incidents") { Task { - do { - let url = try await api.getIncidentsURL() - NSWorkspace.shared.open(url) - } catch { - Logger.shared.error("failed to open 'My Incidents': \(error)") - } + NSWorkspace.shared.open(URL(string: "https://\(subdomain).pagerduty.com/incidents")!) } } Button("My On-Call Shifts") { Task { - do { - let url = try await api.getOnCallShiftsURL() - NSWorkspace.shared.open(url) - } catch { - Logger.shared.error("failed to open 'My On-Call Shifts': \(error)") - } + NSWorkspace.shared.open(URL(string: "https://\(subdomain).pagerduty.com/my-on-call/week")!) } } Divider() diff --git a/PagerCall/SettingsView.swift b/PagerCall/SettingsView.swift index b0eab1d..8e0a559 100644 --- a/PagerCall/SettingsView.swift +++ b/PagerCall/SettingsView.swift @@ -3,6 +3,7 @@ import SwiftUI struct SettingView: View { @State var apiKey: String = Vault.apiKey + @AppStorage("subdomain") private var subdomain = "" @AppStorage("interval") private var interval = Constants.defaultInterval @AppStorage("userID") private var userID = "" @State private var launchAtLogin: Bool = SMAppService.mainApp.status == .enabled @@ -10,10 +11,12 @@ struct SettingView: View { var body: some View { Form { SecureField(text: $apiKey) { - Link("User API Key", destination: URL(string: "https://support.pagerduty.com/main/docs/api-access-keys#section-generate-a-user-token-rest-api-key")!) + Link("API Key", destination: URL(string: "https://support.pagerduty.com/main/docs/api-access-keys")!) }.onChange(of: apiKey) { Vault.apiKey = apiKey } + TextField("Subdomain", text: $subdomain) + TextField("User ID", text: $userID) TextField("Interval (sec)", value: $interval, format: .number.grouping(.never)) .onChange(of: interval) { if interval < 1 { @@ -22,7 +25,6 @@ struct SettingView: View { interval = 3600 } } - TextField("User ID (optional)", text: $userID) Toggle("Launch at login", isOn: $launchAtLogin) .onChange(of: launchAtLogin) { do {