Skip to content

Commit c6fa990

Browse files
committed
fix endpoints
1 parent 9f9753b commit c6fa990

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

Sources/segmentcli/Commands/LivePlugins.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class EdgeFnDeleteCode: Command {
3636
if let error = error {
3737
exitWithError(error)
3838
}
39-
39+
4040
let statusCode = PAPI.shared.statusCode(response: response)
41-
41+
4242
switch statusCode {
4343
case .ok:
4444
// success!
4545
print("Live plugin code deleted for \(self.sourceId.italic.bold).")
46-
46+
4747
case .unauthorized:
4848
fallthrough
4949
case .unauthorized2:
@@ -70,8 +70,6 @@ class EdgeFnUpload: Command {
7070
func execute() throws {
7171
guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return }
7272

73-
var uploadURL: URL? = nil
74-
7573
let fileURL = URL(fileURLWithPath: filePath.expandingTildeInPath)
7674

7775
// Read file contents

Sources/segmentcli/PAPI/PAPIEdgeFunctions.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ extension PAPI {
2727
task.resume()
2828
}
2929

30+
func deleteCode(token: String, sourceId: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
31+
guard var url = URL(string: PAPIEndpoint) else { completion(nil, nil, "Unable to create URL."); return }
32+
33+
url.appendPathComponent(PAPI.Sources.pathEntry)
34+
url.appendPathComponent(sourceId)
35+
url.appendPathComponent(PAPI.EdgeFunctions.pathEntry)
36+
url.appendPathComponent("delete-code")
37+
38+
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
39+
request.httpMethod = "DELETE"
40+
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
41+
42+
let task = URLSession.shared.dataTask(with: request, completionHandler: completion)
43+
task.resume()
44+
}
45+
3046
// http://blah.com/whatever/create?sourceId=1
3147

3248
func createNewVersion(token: String, sourceId: String, code: String, completion: @escaping (Data?, URLResponse?, Error?) -> Void) {
@@ -36,12 +52,27 @@ extension PAPI {
3652
url.appendPathComponent(PAPI.Sources.pathEntry)
3753
url.appendPathComponent(sourceId)
3854
url.appendPathComponent(PAPI.EdgeFunctions.pathEntry)
39-
55+
url.appendPathComponent("create")
56+
4057
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
4158
request.httpMethod = "POST"
4259
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
4360
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
44-
request.httpBody = "{ \"code\": \"\(code)\", \"sourceId\": \"\(sourceId)\" }".data(using: .utf8)
61+
62+
let payload: [String: Any] = [
63+
"code": code,
64+
"sourceId": sourceId
65+
]
66+
67+
do {
68+
request.httpBody = try JSONSerialization.data(withJSONObject: payload, options: [])
69+
if let bodyString = String(data: request.httpBody!, encoding: .utf8) {
70+
print("Request payload: \(bodyString)")
71+
}
72+
} catch {
73+
completion(nil, nil, "Failed to create JSON payload: \(error)")
74+
return
75+
}
4576

4677
let task = URLSession.shared.dataTask(with: request, completionHandler: completion)
4778
task.resume()

0 commit comments

Comments
 (0)