Skip to content

Commit

Permalink
Merge pull request #69 from winebarrel/pa_apiKey
Browse files Browse the repository at this point in the history
Pull around API Key
  • Loading branch information
winebarrel authored Nov 7, 2024
2 parents 9374007 + 67a8298 commit 631487f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions PagerCall.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
CODE_SIGN_ENTITLEMENTS = PagerCall/PagerCall.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
CURRENT_PROJECT_VERSION = 4;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 97A8B2WE2P;
Expand Down Expand Up @@ -324,7 +324,7 @@
CODE_SIGN_ENTITLEMENTS = PagerCall/PagerCall.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 3;
CURRENT_PROJECT_VERSION = 4;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 97A8B2WE2P;
Expand Down
8 changes: 6 additions & 2 deletions PagerCall/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftUI

struct ContentView: View {
@ObservedObject var pagerDuty: PagerDutyModel
@Binding var apiKey: String
@State private var hoverId = ""

var body: some View {
Expand Down Expand Up @@ -56,7 +57,7 @@ struct ContentView: View {
HStack {
Button {
Task {
await pagerDuty.update()
await pagerDuty.update(apiKey)
}
} label: {
Image(systemName: "arrow.triangle.2.circlepath")
Expand All @@ -76,5 +77,8 @@ struct ContentView: View {
}

#Preview {
ContentView(pagerDuty: PagerDutyModel())
ContentView(
pagerDuty: PagerDutyModel(),
apiKey: .constant("")
)
}
6 changes: 3 additions & 3 deletions PagerCall/PagerCallApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PagerCallApp: App {
private func initialize() {
Notification.initialize()

let contentView = ContentView(pagerDuty: pagerDuty)
let contentView = ContentView(pagerDuty: pagerDuty, apiKey: $apiKey)
popover.contentViewController = NSHostingController(rootView: contentView)

scheduleUpdate()
Expand All @@ -42,10 +42,10 @@ struct PagerCallApp: App {
)

timer = Task {
await pagerDuty.update()
await pagerDuty.update(apiKey)

for await _ in seq {
await pagerDuty.update()
await pagerDuty.update(apiKey)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions PagerCall/PagerDutyAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ struct PagerDutyAPI {
struct Oncall: Codable {}
}

func isOnCall() async throws -> Bool {
let data = try await get("/oncalls", ["user_ids[]": userID])
func isOnCall(_ apiKey: String) async throws -> Bool {
let data = try await get(apiKey, "/oncalls", ["user_ids[]": userID])
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let resp = try decoder.decode(OncallsResp.self, from: data)
Expand All @@ -48,8 +48,8 @@ struct PagerDutyAPI {
let incidents: Incidents
}

func getIncidents() async throws -> Incidents {
let data = try await get("/incidents", ["user_ids[]": userID])
func getIncidents(_ apiKey: String) async throws -> Incidents {
let data = try await get(apiKey, "/incidents", ["user_ids[]": userID])
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
Expand All @@ -58,14 +58,14 @@ struct PagerDutyAPI {
return resp.incidents
}

private func get(_ path: String, _ query: [String: String] = [:]) async throws -> Data {
private func get(_ apiKey: String, _ 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) })

var req = URLRequest(url: url)
req.setValue("application/json", forHTTPHeaderField: "Accept")
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.setValue("Token token=\(Vault.apiKey)", forHTTPHeaderField: "Authorization")
req.setValue("Token token=\(apiKey)", forHTTPHeaderField: "Authorization")

let (data, rawResp) = try await URLSession.shared.data(for: req)

Expand Down
6 changes: 3 additions & 3 deletions PagerCall/PagerDutyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class PagerDutyModel: ObservableObject {
@Published var updatedAt: Date?
@Published var error: PagerDutyError?

func update() async {
func update(_ apiKey: String) async {
do {
let onCallNow = try await api.isOnCall()
let currIncidents = try await api.getIncidents()
let onCallNow = try await api.isOnCall(apiKey)
let currIncidents = try await api.getIncidents(apiKey)
let newIncidents = currIncidents - incidents
let hasIncidents = !currIncidents.isEmpty

Expand Down

0 comments on commit 631487f

Please sign in to comment.