Skip to content

Commit

Permalink
Add App Version (#124)
Browse files Browse the repository at this point in the history
* add app version

* update the sdk to point at the new rust code

* add it to the example app

* remove file that shouldnt be committed
  • Loading branch information
nplasterer authored Jul 24, 2023
1 parent 5d6444b commit cbc8bba
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let package = Package(
.package(url: "https://github.com/1024jp/GzipSwift", from: "5.2.0"),
.package(url: "https://github.com/bufbuild/connect-swift", from: "0.3.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/xmtp/xmtp-rust-swift", from: "0.3.0-beta0"),
.package(url: "https://github.com/xmtp/xmtp-rust-swift", from: "0.3.1-beta0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
10 changes: 8 additions & 2 deletions Sources/XMTP/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum ApiClientError: Error {

protocol ApiClient {
var environment: XMTPEnvironment { get }
init(environment: XMTPEnvironment, secure: Bool, rustClient: XMTPRust.RustClient) throws
init(environment: XMTPEnvironment, secure: Bool, rustClient: XMTPRust.RustClient, appVersion: String?) throws
func setAuthToken(_ token: String)
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse
func query(topic: String, pagination: Pagination?, cursor: Xmtp_MessageApi_V1_Cursor?) async throws -> QueryResponse
Expand Down Expand Up @@ -67,10 +67,12 @@ class GRPCApiClient: ApiClient {
var authToken = ""

var rustClient: XMTPRust.RustClient
var appVersion: String

required init(environment: XMTPEnvironment, secure _: Bool = true, rustClient: XMTPRust.RustClient) throws {
required init(environment: XMTPEnvironment, secure _: Bool = true, rustClient: XMTPRust.RustClient, appVersion: String? = nil) throws {
self.environment = environment
self.rustClient = rustClient
self.appVersion = appVersion ?? "0.0.0"
}

static func envToUrl(env: XMTPEnvironment) -> String {
Expand All @@ -87,6 +89,7 @@ class GRPCApiClient: ApiClient {

func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
do {
rustClient.set_app_version(appVersion.intoRustString())
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.batch_query(req)
return try BatchQueryResponse(serializedData: Data(res))
Expand All @@ -97,6 +100,7 @@ class GRPCApiClient: ApiClient {

func query(request: QueryRequest) async throws -> QueryResponse {
do {
rustClient.set_app_version(appVersion.intoRustString())
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.query(req)
return try QueryResponse(serializedData: Data(res))
Expand Down Expand Up @@ -133,6 +137,7 @@ class GRPCApiClient: ApiClient {
func subscribe(topics: [String]) -> AsyncThrowingStream<Envelope, Error> {
return AsyncThrowingStream { continuation in
Task {
rustClient.set_app_version(appVersion.intoRustString())
let request = SubscribeRequest.with { $0.contentTopics = topics }
let req = RustVec<UInt8>(try request.serializedData())
do {
Expand All @@ -156,6 +161,7 @@ class GRPCApiClient: ApiClient {

func publish(request: PublishRequest) async throws -> PublishResponse {
do {
rustClient.set_app_version(appVersion.intoRustString())
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.publish(authToken.intoRustString(), req)
return try PublishResponse(serializedData: Data(res))
Expand Down
8 changes: 6 additions & 2 deletions Sources/XMTP/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public struct ClientOptions {
/// Specify which XMTP network to connect to. Defaults to ``.dev``
public var env: XMTPEnvironment = .dev

/// Specify whether the API client should use TLS security. In general this should only be false when using the `.local` environment.
/// Optional: Specify self-reported version e.g. XMTPInbox/v1.0.0.
public var isSecure: Bool = true

/// Specify whether the API client should use TLS security. In general this should only be false when using the `.local` environment.
public var appVersion: String? = nil

public init(env: XMTPEnvironment = .dev, isSecure: Bool = true) {
public init(env: XMTPEnvironment = .dev, isSecure: Bool = true, appVersion: String? = nil) {
self.env = env
self.isSecure = isSecure
self.appVersion = appVersion
}
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/XMTPTestHelpers/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class FakeApiClient: ApiClient {

public var environment: XMTPEnvironment
public var authToken: String = ""
public var appVersion: String
private var responses: [String: [XMTP.Envelope]] = [:]
private var stream = FakeStreamHolder()
public var published: [XMTP.Envelope] = []
Expand Down Expand Up @@ -119,6 +120,7 @@ public class FakeApiClient: ApiClient {

public init() {
environment = .local
appVersion = "test/0.0.0"
}

public func send(envelope: XMTP.Envelope) {
Expand All @@ -135,8 +137,9 @@ public class FakeApiClient: ApiClient {

// MARK: ApiClient conformance

public required init(environment: XMTP.XMTPEnvironment, secure _: Bool, rustClient _: XMTPRust.RustClient) throws {
public required init(environment: XMTP.XMTPEnvironment, secure _: Bool, rustClient _: XMTPRust.RustClient, appVersion: String?) throws {
self.environment = environment
self.appVersion = appVersion ?? "0.0.0"
}

public func subscribe(topics: [String]) -> AsyncThrowingStream<XMTP.Envelope, Error> {
Expand Down
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.4.0-alpha0"
spec.version = "0.4.1-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -44,7 +44,7 @@ Pod::Spec.new do |spec|
spec.dependency "web3.swift"
spec.dependency "GzipSwift"
spec.dependency "Connect-Swift"
spec.dependency 'XMTPRust', '= 0.3.0-beta0'
spec.dependency 'XMTPRust', '= 0.3.1-beta0'

spec.xcconfig = {'VALID_ARCHS' => 'arm64' }
end
2 changes: 1 addition & 1 deletion XMTPiOSExample/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PODFILE CHECKSUM: e82385142d41677b470dd3362d25b239b9c3621c

COCOAPODS: 1.12.0
COCOAPODS: 1.12.1
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/xmtp-rust-swift",
"state" : {
"revision" : "41a1161cf06a86bab0aa886e450584a1191429b1",
"version" : "0.3.0-beta0"
"revision" : "4a76e5401fa780c40610e2f0d248f695261d08dd",
"version" : "0.3.1-beta0"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion XMTPiOSExample/XMTPiOSExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct ContentView: View {
Task {
do {
let wallet = try PrivateKey.generate()
let client = try await Client.create(account: wallet, options: .init(api: .init(env: .dev, isSecure: true)))
let client = try await Client.create(account: wallet, options: .init(api: .init(env: .dev, isSecure: true, appVersion: "XMTPTest/v1.0.0")))

let keysData = try client.privateKeyBundle.serializedData()
Persistence().saveKeys(keysData)
Expand Down

0 comments on commit cbc8bba

Please sign in to comment.