Skip to content

Commit d883e6b

Browse files
committed
working papi endpoints
1 parent 147c4a4 commit d883e6b

29 files changed

+680
-3339
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/segmentcli.xcscheme

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@
9393
argument = "--help"
9494
isEnabled = "NO">
9595
</CommandLineArgument>
96+
<CommandLineArgument
97+
argument = "sources list"
98+
isEnabled = "NO">
99+
</CommandLineArgument>
100+
<CommandLineArgument
101+
argument = "edgefn latest 3psnN9oWENuLqmfAtKLcKh"
102+
isEnabled = "NO">
103+
</CommandLineArgument>
104+
<CommandLineArgument
105+
argument = "edgefn upload 3psnN9oWENuLqmfAtKLcKh ~/edgefn.js"
106+
isEnabled = "YES">
107+
</CommandLineArgument>
96108
<CommandLineArgument
97109
argument = "scaffold --plugin --swift"
98110
isEnabled = "NO">
@@ -103,7 +115,7 @@
103115
</CommandLineArgument>
104116
<CommandLineArgument
105117
argument = "repl -r /Users/brandonsneed/test.js"
106-
isEnabled = "YES">
118+
isEnabled = "NO">
107119
</CommandLineArgument>
108120
<CommandLineArgument
109121
argument = "auth test1234"

Package.resolved

Lines changed: 19 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PackageDescription
66
let package = Package(
77
name: "segmentcli",
88
platforms: [
9-
.macOS(.v10_15)
9+
.macOS(.v10_15)
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/jakeheis/SwiftCLI", from: "6.0.0"),
@@ -16,7 +16,8 @@ let package = Package(
1616
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.6.1"),
1717
.package(url: "https://github.com/AlwaysRightInstitute/Mustache", from: "1.0.0"),
1818
.package(url: "https://github.com/antitypical/Result.git", from: "5.0.0"),
19-
.package(url: "https://github.com/bsneed/SwiftJS.git", branch: "main")
19+
.package(url: "[email protected]:segmentio/EdgeFn-Swift.git", branch: "main"),
20+
.package(url: "[email protected]:segmentio/substrata-swift.git", branch: "main")
2021
],
2122
targets: [
2223
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -28,7 +29,8 @@ let package = Package(
2829
"ColorizeSwift",
2930
"SwiftCSV",
3031
"Result",
31-
"SwiftJS",
32+
.product(name: "Substrata", package: "substrata-swift"),
33+
.product(name: "EdgeFn", package: "EdgeFn-Swift"),
3234
.product(name: "mustache", package: "Mustache"),
3335
.product(name: "Segment", package: "analytics-swift")]),
3436
.testTarget(
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Brandon Sneed on 7/7/22.
6+
//
7+
8+
import Foundation
9+
import SwiftCLI
10+
import Spinner
11+
import ColorizeSwift
12+
import Segment
13+
14+
class EdgeFnGroup: CommandGroup {
15+
let name = "edgefn"
16+
let shortDescription = "Work with and develop edge functions"
17+
let children: [Routable] = [EdgeFnLatestCommand(), EdgeFnUpload(), EdgeFnDisable()]
18+
init() {}
19+
}
20+
21+
class EdgeFnDisable: Command {
22+
let name = "disable"
23+
let shortDescription = "Disable edge functions for a given source ID"
24+
25+
@Param var sourceId: String
26+
27+
func execute() throws {
28+
guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return }
29+
executeAndWait { semaphore in
30+
let spinner = Spinner(.dots, "Uploading edge function ...")
31+
spinner.start()
32+
33+
PAPI.shared.edgeFunctions.disable(token: workspace.token, sourceId: sourceId) { data, response, error in
34+
spinner.stop()
35+
36+
if let error = error {
37+
exitWithError(error)
38+
}
39+
40+
let statusCode = PAPI.shared.statusCode(response: response)
41+
42+
switch statusCode {
43+
case .ok:
44+
// success!
45+
print("Edge functions disabled for \(self.sourceId.italic.bold).")
46+
47+
case .unauthorized:
48+
fallthrough
49+
case .unauthorized2:
50+
exitWithError(code: .commandFailed, message: "Supplied token is not authorized.")
51+
case .notFound:
52+
exitWithError(code: .commandFailed, message: "No edge functions were found.")
53+
default:
54+
exitWithError("An unknown error occurred.")
55+
}
56+
semaphore.signal()
57+
}
58+
}
59+
}
60+
}
61+
62+
63+
class EdgeFnUpload: Command {
64+
let name = "upload"
65+
let shortDescription = "Upload an edge function"
66+
67+
@Param var sourceId: String
68+
@Param var filePath: String
69+
70+
func execute() throws {
71+
guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return }
72+
73+
var uploadURL: URL? = nil
74+
75+
let fileURL = URL(fileURLWithPath: filePath.expandingTildeInPath)
76+
77+
// generate upload URL
78+
executeAndWait { semaphore in
79+
let spinner = Spinner(.dots, "Generating upload URL ...")
80+
spinner.start()
81+
82+
PAPI.shared.edgeFunctions.generateUploadURL(token: workspace.token, sourceId: sourceId) { data, response, error in
83+
spinner.stop()
84+
85+
if let error = error {
86+
exitWithError(error)
87+
}
88+
89+
let statusCode = PAPI.shared.statusCode(response: response)
90+
91+
switch statusCode {
92+
case .ok:
93+
// success!
94+
if let jsonData = data, let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] {
95+
if let uploadString = json[keyPath: "data.uploadURL"] as? String {
96+
uploadURL = URL(string: uploadString)
97+
}
98+
}
99+
100+
case .unauthorized:
101+
fallthrough
102+
case .unauthorized2:
103+
exitWithError(code: .commandFailed, message: "Supplied token is not authorized.")
104+
case .notFound:
105+
exitWithError(code: .commandFailed, message: "No edge functions were found.")
106+
default:
107+
exitWithError("An unknown error occurred.")
108+
}
109+
semaphore.signal()
110+
}
111+
}
112+
113+
// upload it to the URL we were given.
114+
executeAndWait { semaphore in
115+
let spinner = Spinner(.dots, "Uploading \(fileURL.lastPathComponent) ...")
116+
spinner.start()
117+
118+
PAPI.shared.edgeFunctions.uploadToGeneratedURL(token: workspace.token, url: uploadURL, fileURL: fileURL) { data, response, error in
119+
spinner.stop()
120+
121+
if let error = error {
122+
exitWithError(error)
123+
}
124+
125+
let statusCode = PAPI.shared.statusCode(response: response)
126+
127+
switch statusCode {
128+
case .ok:
129+
// success!
130+
break
131+
132+
case .unauthorized:
133+
fallthrough
134+
case .unauthorized2:
135+
exitWithError(code: .commandFailed, message: "Supplied token is not authorized.")
136+
case .notFound:
137+
exitWithError(code: .commandFailed, message: "No edge functions were found.")
138+
default:
139+
exitWithError("An unknown error occurred.")
140+
}
141+
semaphore.signal()
142+
}
143+
}
144+
145+
// call create to make a new connection to the version we just posted.
146+
executeAndWait { semaphore in
147+
let spinner = Spinner(.dots, "Creating new edge function version ...")
148+
spinner.start()
149+
150+
PAPI.shared.edgeFunctions.createNewVersion(token: workspace.token, sourceId: sourceId, uploadURL: uploadURL) { data, response, error in
151+
spinner.stop()
152+
153+
if let error = error {
154+
exitWithError(error)
155+
}
156+
157+
let statusCode = PAPI.shared.statusCode(response: response)
158+
159+
switch statusCode {
160+
case .ok:
161+
// success!
162+
if let jsonData = data, let jsonObject = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] {
163+
if let json = try? JSON(jsonObject) {
164+
print(json.prettyPrint())
165+
}
166+
}
167+
168+
case .unauthorized:
169+
fallthrough
170+
case .unauthorized2:
171+
exitWithError(code: .commandFailed, message: "Supplied token is not authorized.")
172+
case .notFound:
173+
exitWithError(code: .commandFailed, message: "No edge functions were found.")
174+
default:
175+
exitWithError("An unknown error occurred.")
176+
}
177+
semaphore.signal()
178+
}
179+
}
180+
181+
}
182+
}
183+
184+
class EdgeFnLatestCommand: Command {
185+
let name = "latest"
186+
let shortDescription = "Get info about the latest Edge Function in use"
187+
188+
@Param var sourceId: String
189+
190+
func execute() throws {
191+
guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return }
192+
193+
executeAndWait { semaphore in
194+
let spinner = Spinner(.dots, "Retrieving latest Edge Functino info ...")
195+
spinner.start()
196+
197+
PAPI.shared.edgeFunctions.latest(token: workspace.token, sourceId: sourceId) { data, response, error in
198+
spinner.stop()
199+
200+
if let error = error {
201+
exitWithError(error)
202+
}
203+
204+
let statusCode = PAPI.shared.statusCode(response: response)
205+
206+
switch statusCode {
207+
case .ok:
208+
// success!
209+
if let jsonData = data, let jsonObject = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] {
210+
if let json = try? JSON(jsonObject) {
211+
print(json.prettyPrint())
212+
}
213+
}
214+
215+
case .unauthorized:
216+
fallthrough
217+
case .unauthorized2:
218+
exitWithError(code: .commandFailed, message: "Supplied token is not authorized.")
219+
case .notFound:
220+
exitWithError(code: .commandFailed, message: "No edge functions were found.")
221+
default:
222+
exitWithError("An unknown error occurred.")
223+
}
224+
semaphore.signal()
225+
}
226+
}
227+
}
228+
}
229+

Sources/segmentcli/Commands/Import.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ class ImportCommand: Command {
2323
let generate = Mustache(importer_js)
2424
let result = generate(name: "", writeKey: writeKey, csvFile: csvFile)
2525

26-
runJS(script: result, context: jsContext)
26+
runJS(script: result)
2727
}
2828
}

0 commit comments

Comments
 (0)