Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if [[ -z $DISABLE_SWIFT ]]; then
if command -v swift &> /dev/null
then
echo "Installing swift protoc plugin"
git clone --depth 1 --branch 1.22.0 https://github.com/apple/swift-protobuf $PROTOC_DIR/.swift-protobuf
git clone --depth 1 --branch "${PROTO_SWIFT_VERSION}" https://github.com/apple/swift-protobuf $PROTOC_DIR/.swift-protobuf
(cd $PROTOC_DIR/.swift-protobuf && swift build -c release)
ln -s $PROTOC_DIR/.swift-protobuf/.build/release/protoc-gen-swift $PROTOC_DIR/bin/protoc-gen-swift
else
Expand Down
55 changes: 38 additions & 17 deletions tools/protoc-gen-swift-twirp/generator/template.goswift
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,67 @@ import {{.Path}};

{{range .Services}}
class {{.ClassName}}: @unchecked Sendable {
private let httpClient: HTTPClient
let hostname: String
var token: String
let apiKey: String
private let httpClient: HTTPClient
let hostname: String
var token: String
let apiKey: String
let syncQueue = DispatchQueue(label: "{{.ClassName}}", qos: .userInitiated)
let pathPrefix: String = "/{{.Package}}.{{.Name}}/"
init(httpClient: HTTPClient, apiKey: String, hostname: String, token: String) {
let pathPrefix: String = "/{{.Package}}.{{.Name}}/"
var httpConfig = HTTPConfig.default //TODO: move this

init(httpClient: HTTPClient, apiKey: String, hostname: String, token: String) {
self.httpClient = httpClient
self.hostname = hostname
self.token = token
self.hostname = hostname
self.token = token
self.apiKey = apiKey
}
}
{{range .Methods}}
func {{.Name}}({{.InputArg}}: {{.InputType}}) async throws -> {{.OutputType}} {
func {{.Name}}({{.InputArg}}: {{.InputType}}) async throws -> {{.OutputType}} {
return try await execute(request: {{.InputArg}}, path: "{{.Path}}")
}
}
{{end}}
func update(userToken: String) {
syncQueue.async { [weak self] in
self?.token = userToken
}
}

private func execute<Request: ProtoModel, Response: ProtoModel>(request: Request, path: String) async throws -> Response {
private func execute<Request: ProtoModel, Response: ProtoModelResponse>(request: Request, path: String, retries: Int = 0) async throws -> Response {
let requestData = try request.serializedData()
var request = try makeRequest(for: path)
request.httpBody = requestData
let responseData = try await httpClient.execute(request: request)
let response = try Response.init(serializedData: responseData)
var urlRequest = try makeRequest(for: path)
urlRequest.httpBody = requestData
let responseData = try await httpClient.execute(request: urlRequest)
let response = try Response.init(serializedBytes: responseData)
if response.hasError {
if response.error.shouldRetry && retries < httpConfig.maxRetries {
let delay = httpConfig.retryStrategy.getDelayAfterTheFailure()
let delayNanoseconds = UInt64(delay * 1_000_000_000)
log.debug("Delaying retry for \(delay) seconds")
try await Task.sleep(nanoseconds: delayNanoseconds)
log.debug("Retrying request for path \(path)")
return try await execute(request: request, path: path, retries: retries + 1)
} else {
httpConfig.retryStrategy.resetConsecutiveFailures()
throw NSError(
domain: "stream",
code: response.error.code.rawValue,
userInfo: ["message": response.error.message]
)
}
}
httpConfig.retryStrategy.resetConsecutiveFailures()
return response
}

private func makeRequest(for path: String) throws -> URLRequest {
let url = hostname + pathPrefix + path + "?api_key=\(apiKey)"
let url = hostname + pathPrefix + path + "?api_key=\(apiKey)"
guard let url = URL(string: url) else {
throw NSError(domain: "stream", code: 123)
}
var request = URLRequest(url: url)
request.setValue("application/protobuf", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "authorization")
request.setValue(SystemEnvironment.sdkIdentifier, forHTTPHeaderField: "X-Stream-Client")
request.httpMethod = "POST"
return request
}
Expand Down
6 changes: 6 additions & 0 deletions versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export PROTO_TWIRP_VERSION=v8.1.2
export PROTO_GO_VERSION=v1.28.1
export PROTO_VTPROTO_VERSION=v0.3.0
export PROTO_LINT_VERSION=v0.39.0
# Must match the SwiftProtobuf runtime version that the consuming Swift SDKs
# link against (see stream-video-swift Package.swift). Generating with an older
# protoc-gen-swift emits code that trips Swift 6 concurrency checking
# (`static var allCases`, non-Sendable `_StorageClass`) and deprecated APIs
# (`init(serializedData:)`).
export PROTO_SWIFT_VERSION=1.38.1
Loading