Skip to content

Commit

Permalink
Run swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Jul 19, 2023
1 parent 1d086cd commit a3fcc06
Show file tree
Hide file tree
Showing 36 changed files with 890 additions and 905 deletions.
160 changes: 80 additions & 80 deletions Sources/XMTP/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,44 @@ typealias QueryResponse = Xmtp_MessageApi_V1_QueryResponse
typealias SubscribeRequest = Xmtp_MessageApi_V1_SubscribeRequest

public enum ApiClientError: Error {
case batchQueryError(String)
case queryError(String)
case publishError(String)
case subscribeError(String)
case batchQueryError(String)
case queryError(String)
case publishError(String)
case subscribeError(String)
}

protocol ApiClient {
var environment: XMTPEnvironment { get }
init(environment: XMTPEnvironment, secure: Bool, rustClient: XMTPRust.RustClient) throws
func setAuthToken(_ token: String)
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse
func query(topic: String, pagination: Pagination?, cursor: Xmtp_MessageApi_V1_Cursor?) async throws -> QueryResponse
func query(topic: Topic, pagination: Pagination?) async throws -> QueryResponse
func query(request: QueryRequest) async throws -> QueryResponse
func query(request: QueryRequest) async throws -> QueryResponse
func envelopes(topic: String, pagination: Pagination?) async throws -> [Envelope]
func publish(envelopes: [Envelope]) async throws -> PublishResponse
func publish(request: PublishRequest) async throws -> PublishResponse
func publish(request: PublishRequest) async throws -> PublishResponse
func subscribe(topics: [String]) -> AsyncThrowingStream<Envelope, Error>
}

func makeQueryRequest(topic: String, pagination: Pagination? = nil, cursor: Cursor? = nil) -> QueryRequest {
return QueryRequest.with {
$0.contentTopics = [topic]
if let pagination {
$0.pagingInfo = pagination.pagingInfo
}
if let endAt = pagination?.before {
$0.endTimeNs = UInt64(endAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
}
if let startAt = pagination?.after {
$0.startTimeNs = UInt64(startAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
}
if let cursor {
$0.pagingInfo.cursor = cursor
}
}
return QueryRequest.with {
$0.contentTopics = [topic]
if let pagination {
$0.pagingInfo = pagination.pagingInfo
}
if let endAt = pagination?.before {
$0.endTimeNs = UInt64(endAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
}
if let startAt = pagination?.after {
$0.startTimeNs = UInt64(startAt.millisecondsSinceEpoch) * 1_000_000
$0.pagingInfo.direction = .descending
}
if let cursor {
$0.pagingInfo.cursor = cursor
}
}
}

class GRPCApiClient: ApiClient {
Expand Down Expand Up @@ -85,33 +85,33 @@ class GRPCApiClient: ApiClient {
authToken = token
}

func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.batch_query(req)
return try BatchQueryResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.batchQueryError(error.toString())
}
}

func query(request: QueryRequest) async throws -> QueryResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.query(req)
return try QueryResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.queryError(error.toString())
}
}

func query(topic: String, pagination: Pagination? = nil, cursor: Cursor? = nil) async throws -> QueryResponse {
return try await query(request: makeQueryRequest(topic: topic, pagination: pagination, cursor: cursor))
}

func query(topic: Topic, pagination: Pagination? = nil) async throws -> QueryResponse {
return try await query(request: makeQueryRequest(topic: topic.description, pagination: pagination))
}
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.batch_query(req)
return try BatchQueryResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.batchQueryError(error.toString())
}
}

func query(request: QueryRequest) async throws -> QueryResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.query(req)
return try QueryResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.queryError(error.toString())
}
}

func query(topic: String, pagination: Pagination? = nil, cursor: Cursor? = nil) async throws -> QueryResponse {
return try await query(request: makeQueryRequest(topic: topic, pagination: pagination, cursor: cursor))
}

func query(topic: Topic, pagination: Pagination? = nil) async throws -> QueryResponse {
return try await query(request: makeQueryRequest(topic: topic.description, pagination: pagination))
}

func envelopes(topic: String, pagination: Pagination? = nil) async throws -> [Envelope] {
var envelopes: [Envelope] = []
Expand All @@ -133,40 +133,40 @@ class GRPCApiClient: ApiClient {
func subscribe(topics: [String]) -> AsyncThrowingStream<Envelope, Error> {
return AsyncThrowingStream { continuation in
Task {
let request = SubscribeRequest.with { $0.contentTopics = topics }
let req = RustVec<UInt8>(try request.serializedData())
do {
let subscription = try await self.rustClient.subscribe(req)
// Run a continuous for loop polling and sleeping for a bit each loop.
while true {
let buf = try subscription.get_envelopes_as_query_response()
// Note: it uses QueryResponse as a convenient envelopes wrapper.
let res = try QueryResponse(serializedData: Data(buf))
for envelope in res.envelopes {
continuation.yield(envelope)
}
try await Task.sleep(nanoseconds: 50_000_000) // 50ms
}
} catch let error as RustString {
throw ApiClientError.subscribeError(error.toString())
}
let request = SubscribeRequest.with { $0.contentTopics = topics }
let req = RustVec<UInt8>(try request.serializedData())
do {
let subscription = try await self.rustClient.subscribe(req)
// Run a continuous for loop polling and sleeping for a bit each loop.
while true {
let buf = try subscription.get_envelopes_as_query_response()
// Note: it uses QueryResponse as a convenient envelopes wrapper.
let res = try QueryResponse(serializedData: Data(buf))
for envelope in res.envelopes {
continuation.yield(envelope)
}
try await Task.sleep(nanoseconds: 50_000_000) // 50ms
}
} catch let error as RustString {
throw ApiClientError.subscribeError(error.toString())
}
}
}
}

func publish(request: PublishRequest) async throws -> PublishResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.publish(authToken.intoRustString(), req)
return try PublishResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.publishError(error.toString())
}
}
func publish(request: PublishRequest) async throws -> PublishResponse {
do {
let req = RustVec<UInt8>(try request.serializedData())
let res: RustVec<UInt8> = try await rustClient.publish(authToken.intoRustString(), req)
return try PublishResponse(serializedData: Data(res))
} catch let error as RustString {
throw ApiClientError.publishError(error.toString())
}
}

@discardableResult func publish(envelopes: [Envelope]) async throws -> PublishResponse {
return try await publish(request: PublishRequest.with {
$0.envelopes = envelopes
})
return try await publish(request: PublishRequest.with {
$0.envelopes = envelopes
})
}
}
42 changes: 21 additions & 21 deletions Sources/XMTP/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import web3
import XMTPRust

public enum ClientError: Error {
case creationError(String)
case creationError(String)
}

/// Specify configuration options for creating a ``Client``.
Expand Down Expand Up @@ -78,17 +78,17 @@ public class Client {
/// Creates a client.
public static func create(account: SigningKey, options: ClientOptions? = nil) async throws -> Client {
let options = options ?? ClientOptions()
do {
let client = try await XMTPRust.create_client(GRPCApiClient.envToUrl(env: options.api.env), options.api.env != .local)
let apiClient = try GRPCApiClient(
environment: options.api.env,
secure: options.api.isSecure,
rustClient: client
)
return try await create(account: account, apiClient: apiClient)
} catch let error as RustString {
throw ClientError.creationError(error.toString())
}
do {
let client = try await XMTPRust.create_client(GRPCApiClient.envToUrl(env: options.api.env), options.api.env != .local)
let apiClient = try GRPCApiClient(
environment: options.api.env,
secure: options.api.isSecure,
rustClient: client
)
return try await create(account: account, apiClient: apiClient)
} catch let error as RustString {
throw ClientError.creationError(error.toString())
}
}

static func create(account: SigningKey, apiClient: ApiClient) async throws -> Client {
Expand All @@ -104,7 +104,7 @@ public class Client {
// swiftlint:disable no_optional_try
if let keys = try await loadPrivateKeys(for: account, apiClient: apiClient) {
// swiftlint:enable no_optional_try
print("loading existing private keys.")
print("loading existing private keys.")
#if DEBUG
print("Loaded existing private keys.")
#endif
Expand Down Expand Up @@ -140,10 +140,10 @@ public class Client {
for envelope in res.envelopes {
let encryptedBundle = try EncryptedPrivateKeyBundle(serializedData: envelope.message)
let bundle = try await encryptedBundle.decrypted(with: account)
if case .v1 = bundle.version {
return bundle.v1
}
print("discarding unsupported stored key bundle")
if case .v1 = bundle.version {
return bundle.v1
}
print("discarding unsupported stored key bundle")
}

return nil
Expand Down Expand Up @@ -176,7 +176,7 @@ public class Client {
}

public func enableGroupChat() {
self.isGroupChatEnabled = true
isGroupChatEnabled = true
GroupChat.registerCodecs()
}

Expand Down Expand Up @@ -291,9 +291,9 @@ public class Client {
)
}

func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
return try await apiClient.batchQuery(request: request)
}
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
return try await apiClient.batchQuery(request: request)
}

@discardableResult func publish(envelopes: [Envelope]) async throws -> PublishResponse {
let authorized = AuthorizedIdentity(address: address, authorized: privateKeyBundleV1.identityKey.publicKey, identity: privateKeyBundleV1.identityKey)
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTP/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public enum Conversation: Sendable {
/// See Conversations.importTopicData()
public func toTopicData() -> Xmtp_KeystoreApi_V1_TopicMap.TopicData {
Xmtp_KeystoreApi_V1_TopicMap.TopicData.with {
$0.createdNs = UInt64(createdAt.timeIntervalSince1970 * 1_000) * 1_000_000
$0.createdNs = UInt64(createdAt.timeIntervalSince1970 * 1000) * 1_000_000
$0.peerAddress = peerAddress
if case let .v2(cv2) = self {
$0.invitation = Xmtp_MessageContents_InvitationV1.with {
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTP/ConversationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public struct ConversationV1 {
let pagination = Pagination(limit: limit, before: before, after: after)

let envelopes = try await client.apiClient.envelopes(
topic: Topic.directMessageV1(client.address, peerAddress).description,
topic: Topic.directMessageV1(client.address, peerAddress).description,
pagination: pagination
)

Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTP/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public struct ConversationV2 {

return envelopes.compactMap { envelope in
do {
return try decode(envelope: envelope)
return try decode(envelope: envelope)
} catch {
print("Error decoding envelope \(error)")
return nil
Expand Down
Loading

0 comments on commit a3fcc06

Please sign in to comment.