Skip to content

Commit 631487f

Browse files
authored
Merge pull request #69 from winebarrel/pa_apiKey
Pull around API Key
2 parents 9374007 + 67a8298 commit 631487f

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

PagerCall.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
CODE_SIGN_ENTITLEMENTS = PagerCall/PagerCall.entitlements;
294294
CODE_SIGN_STYLE = Automatic;
295295
COMBINE_HIDPI_IMAGES = YES;
296-
CURRENT_PROJECT_VERSION = 3;
296+
CURRENT_PROJECT_VERSION = 4;
297297
DEAD_CODE_STRIPPING = YES;
298298
DEVELOPMENT_ASSET_PATHS = "";
299299
DEVELOPMENT_TEAM = 97A8B2WE2P;
@@ -324,7 +324,7 @@
324324
CODE_SIGN_ENTITLEMENTS = PagerCall/PagerCall.entitlements;
325325
CODE_SIGN_STYLE = Automatic;
326326
COMBINE_HIDPI_IMAGES = YES;
327-
CURRENT_PROJECT_VERSION = 3;
327+
CURRENT_PROJECT_VERSION = 4;
328328
DEAD_CODE_STRIPPING = YES;
329329
DEVELOPMENT_ASSET_PATHS = "";
330330
DEVELOPMENT_TEAM = 97A8B2WE2P;

PagerCall/ContentView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import SwiftUI
22

33
struct ContentView: View {
44
@ObservedObject var pagerDuty: PagerDutyModel
5+
@Binding var apiKey: String
56
@State private var hoverId = ""
67

78
var body: some View {
@@ -56,7 +57,7 @@ struct ContentView: View {
5657
HStack {
5758
Button {
5859
Task {
59-
await pagerDuty.update()
60+
await pagerDuty.update(apiKey)
6061
}
6162
} label: {
6263
Image(systemName: "arrow.triangle.2.circlepath")
@@ -76,5 +77,8 @@ struct ContentView: View {
7677
}
7778

7879
#Preview {
79-
ContentView(pagerDuty: PagerDutyModel())
80+
ContentView(
81+
pagerDuty: PagerDutyModel(),
82+
apiKey: .constant("")
83+
)
8084
}

PagerCall/PagerCallApp.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct PagerCallApp: App {
2727
private func initialize() {
2828
Notification.initialize()
2929

30-
let contentView = ContentView(pagerDuty: pagerDuty)
30+
let contentView = ContentView(pagerDuty: pagerDuty, apiKey: $apiKey)
3131
popover.contentViewController = NSHostingController(rootView: contentView)
3232

3333
scheduleUpdate()
@@ -42,10 +42,10 @@ struct PagerCallApp: App {
4242
)
4343

4444
timer = Task {
45-
await pagerDuty.update()
45+
await pagerDuty.update(apiKey)
4646

4747
for await _ in seq {
48-
await pagerDuty.update()
48+
await pagerDuty.update(apiKey)
4949
}
5050
}
5151
}

PagerCall/PagerDutyAPI.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct PagerDutyAPI {
3535
struct Oncall: Codable {}
3636
}
3737

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

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

61-
private func get(_ path: String, _ query: [String: String] = [:]) async throws -> Data {
61+
private func get(_ apiKey: String, _ path: String, _ query: [String: String] = [:]) async throws -> Data {
6262
var url = endpoint.appendingPathComponent(path)
6363
url.append(queryItems: query.map { key, val in URLQueryItem(name: key, value: val) })
6464

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

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

PagerCall/PagerDutyModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class PagerDutyModel: ObservableObject {
1717
@Published var updatedAt: Date?
1818
@Published var error: PagerDutyError?
1919

20-
func update() async {
20+
func update(_ apiKey: String) async {
2121
do {
22-
let onCallNow = try await api.isOnCall()
23-
let currIncidents = try await api.getIncidents()
22+
let onCallNow = try await api.isOnCall(apiKey)
23+
let currIncidents = try await api.getIncidents(apiKey)
2424
let newIncidents = currIncidents - incidents
2525
let hasIncidents = !currIncidents.isEmpty
2626

0 commit comments

Comments
 (0)