-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Use Swift-Bridge generated files with libxmtp XCFramework to run gRPC query #1
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5199313
wip: attempt at getting Swift-Bridge into a Package.swift
5f5e478
feat: build works surprisingly, needed to package generated headers i…
fa0882e
feat: no Bridge necessary
cdea13e
feat: tests work
a0133c1
feat: implement ApiService
f8e5ff8
style: just make the sources path explicit
a91874a
feat: add subscribe_once function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
// swift-tools-version:5.3 | ||
import PackageDescription | ||
import Foundation | ||
|
||
let package = Package( | ||
name: "XMTPRustSwift", | ||
name: "XMTPRust", | ||
platforms: [ | ||
.iOS(.v13), | ||
.macOS(.v11) | ||
], | ||
products: [ | ||
.library( | ||
name: "XMTPRustSwift", | ||
targets: ["XMTPRustSwift"]), | ||
name: "XMTPRust", | ||
targets: ["XMTPRust", "XMTPRustSwift"]), | ||
], | ||
targets: [ | ||
.target( | ||
name: "XMTPRust", | ||
dependencies: ["XMTPRustSwift"], | ||
path: "Sources/XMTPRust" | ||
), | ||
.binaryTarget( | ||
name: "XMTPRustSwift", | ||
path: "XMTPRustSwift.xcframework"), | ||
.testTarget( | ||
name: "XMTPRust-Tests", | ||
dependencies: ["XMTPRust"], | ||
path: "Tests/XMTPRust-Tests") | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// ApiService.swift | ||
// | ||
// | ||
// Created by Michael Xu on 4/14/23. | ||
// | ||
|
||
import Foundation | ||
|
||
// The goal of this class is to provide a steady-ish Swift interface between Rust and xmtp-ios | ||
public class ApiService { | ||
static let shared = ApiService() | ||
|
||
let environment: String | ||
let secure: Bool | ||
|
||
public init(environment: String = "https://dev.xmtp.network", secure: Bool = true) { | ||
self.environment = environment | ||
self.secure = secure | ||
} | ||
|
||
// shared getter | ||
public static func get() -> ApiService { | ||
return ApiService.shared | ||
} | ||
|
||
// It's all strings all the time | ||
public func query(topic: String, json_paging_info: String) async throws -> String { | ||
// Call XMTPRust.query_serialized with the given parameters, expect a XMTPRust.ResponseJson object | ||
// that has { error: String, json: String } | ||
let response: ResponseJson = await XMTPRust.query(self.environment.intoRustString(), topic.intoRustString(), json_paging_info.intoRustString()) | ||
|
||
// If the error is not an empty string | ||
if response.error.toString() != "" { | ||
throw NSError(domain: "XMTPRust", code: 0, userInfo: [NSLocalizedDescriptionKey: response.error.toString()]) | ||
} | ||
|
||
return response.json.toString() | ||
} | ||
|
||
// Publishes a string, which better be an encoded Xmtp_MessageApi_V1_PublishRequest with envelopes provided | ||
public func publish(token: String, envelopes: String) async throws -> String { | ||
// Call XMTPRust.publish_serialized with the given parameters, expect a XMTPRust.ResponseJson object | ||
// that has { error: String, json: String } | ||
let response: ResponseJson = await XMTPRust.publish(self.environment.intoRustString(), token.intoRustString(), envelopes.intoRustString()) | ||
|
||
// If the error is not an empty string | ||
if response.error.toString() != "" { | ||
throw NSError(domain: "XMTPRust", code: 0, userInfo: [NSLocalizedDescriptionKey: response.error.toString()]) | ||
} | ||
|
||
return response.json.toString() | ||
} | ||
|
||
// Subscribe, we need to fake an AsyncThrowingStream by cleverly using a DispatchQueue and repeatedly | ||
// calling an async function that will call XMTPRust.subscribe_serialized and return a string | ||
// that better be an encoded Xmtp_MessageApi_V1_SubscribeResponse with envelopes provided | ||
func subscribe(topics: [String]) -> AsyncThrowingStream<String, Error> { | ||
return AsyncThrowingStream { continuation in | ||
// Create a DispatchQueue | ||
let queue = DispatchQueue(label: "XMTPRust.subscribe_serialized") | ||
// Run a constant for loop that calls subscribe_once over and over | ||
queue.async { | ||
Task { | ||
let vec = RustVec<RustString>() | ||
for topic in topics { | ||
vec.push(value: topic.intoRustString()) | ||
} | ||
// Call XMTPRust.subscribe_serialized with the given parameters, expect a String | ||
// that better be an encoded Xmtp_MessageApi_V1_SubscribeResponse with envelopes provided | ||
let response = await XMTPRust.subscribe_once(self.environment.intoRustString(), vec) | ||
|
||
// If the error is not an empty string | ||
if response.error.toString() != "" { | ||
continuation.finish(throwing: NSError(domain: "XMTPRust", code: 0, userInfo: [NSLocalizedDescriptionKey: response.error.toString()])) | ||
} | ||
|
||
// Yield the response | ||
continuation.yield(response.json.toString()) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swifty-ness would say we don't need this getter since we've already got
ApiService.shared
, we could just make that public.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah good call!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix for future releases