Skip to content
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

chore: improve pattern matching experience for response types #90

Merged
merged 12 commits into from
Dec 12, 2023
337 changes: 180 additions & 157 deletions Sources/Momento/CacheClient.swift

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions Sources/Momento/TopicClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@available(macOS 10.15, iOS 13, *)
public protocol TopicClientProtocol {
func publish(
cacheName: String,
Expand Down Expand Up @@ -107,10 +108,10 @@ public class TopicClient: TopicClientProtocol {
try validateCacheName(cacheName: cacheName)
try validateTopicName(topicName: topicName)
} catch let err as SdkError {
return TopicPublishError(error: err)
return TopicPublishResponse.error(TopicPublishError(error: err))
} catch {
return TopicPublishError(error: UnknownError(
message: "unexpected error: \(error)")
return TopicPublishResponse.error(
TopicPublishError(error: UnknownError(message: "unexpected error: \(error)"))
)
}

Expand All @@ -122,10 +123,12 @@ public class TopicClient: TopicClientProtocol {
)
return result
} catch {
return TopicPublishError(
error: UnknownError(
message: "Unknown error from publish",
innerException: error
return TopicPublishResponse.error(
TopicPublishError(
error: UnknownError(
message: "Unknown error from publish",
innerException: error
)
)
)
}
Expand Down Expand Up @@ -153,10 +156,10 @@ public class TopicClient: TopicClientProtocol {
try validateCacheName(cacheName: cacheName)
try validateTopicName(topicName: topicName)
} catch let err as SdkError {
return TopicSubscribeError(error: err)
return TopicSubscribeResponse.error(TopicSubscribeError(error: err))
} catch {
return TopicSubscribeError(error: UnknownError(
message: "unexpected error: \(error)")
return TopicSubscribeResponse.error(
TopicSubscribeError(error: UnknownError(message: "unexpected error: \(error)"))
)
}

Expand All @@ -167,12 +170,12 @@ public class TopicClient: TopicClientProtocol {
)
return result
} catch {
return TopicSubscribeError(
return TopicSubscribeResponse.error(TopicSubscribeError(
error: UnknownError(
message: "Unknown error from subscribe",
innerException: error
)
)
))
}
}

Expand Down
56 changes: 37 additions & 19 deletions Sources/Momento/internal/ControlClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import NIOHPACK
import Logging

protocol ControlClientProtocol {
func createCache(cacheName: String) async -> CacheCreateResponse
func createCache(cacheName: String) async -> CreateCacheResponse

func deleteCache(cacheName: String) async -> CacheDeleteResponse
func deleteCache(cacheName: String) async -> DeleteCacheResponse

func listCaches() async -> CacheListResponse
func listCaches() async -> ListCachesResponse
}

@available(macOS 10.15, iOS 13, *)
Expand Down Expand Up @@ -59,56 +59,74 @@ class ControlClient: ControlClientProtocol {
)
}

func createCache(cacheName: String) async -> CacheCreateResponse {
func createCache(cacheName: String) async -> CreateCacheResponse {
var request = ControlClient__CreateCacheRequest()
request.cacheName = cacheName
let call = self.client.createCache(request)
do {
_ = try await call.response.get()
// Successful creation returns ControlClient__CreateCacheResponse
return CacheCreateSuccess()
return CreateCacheResponse.success(CreateCacheSuccess())
} catch let err as GRPCStatus {
if err.code == GRPCStatus.Code.alreadyExists {
return CacheCreateCacheAlreadyExists()
return CreateCacheResponse.alreadyExists(CreateCacheAlreadyExists())
}
return CacheCreateError(error: grpcStatusToSdkError(grpcStatus: err))
return CreateCacheResponse.error(
CreateCacheError(error: grpcStatusToSdkError(grpcStatus: err))
)
} catch let err as GRPCConnectionPoolError {
return CacheCreateError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
return CreateCacheResponse.error(
CreateCacheError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
)
} catch {
return CacheCreateError(error: UnknownError(message: "unknown cache create error \(error)"))
return CreateCacheResponse.error(
CreateCacheError(error: UnknownError(message: "unknown cache create error \(error)"))
)
}
}

func deleteCache(cacheName: String) async -> CacheDeleteResponse {
func deleteCache(cacheName: String) async -> DeleteCacheResponse {
var request = ControlClient__DeleteCacheRequest()
request.cacheName = cacheName
let call = self.client.deleteCache(request)
do {
_ = try await call.response.get()
// Successful creation returns ControlClient__DeleteCacheResponse
return CacheDeleteSuccess()
return DeleteCacheResponse.success(DeleteCacheSuccess())
} catch let err as GRPCStatus {
return CacheDeleteError(error: grpcStatusToSdkError(grpcStatus: err))
return DeleteCacheResponse.error(
DeleteCacheError(error: grpcStatusToSdkError(grpcStatus: err))
)
} catch let err as GRPCConnectionPoolError {
return CacheDeleteError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
return DeleteCacheResponse.error(
DeleteCacheError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
)
} catch {
return CacheDeleteError(error: UnknownError(message: "unknown cache create error \(error)"))
return DeleteCacheResponse.error(
DeleteCacheError(error: UnknownError(message: "unknown cache create error \(error)"))
)
}
}

func listCaches() async -> CacheListResponse {
func listCaches() async -> ListCachesResponse {
let call = self.client.listCaches(ControlClient__ListCachesRequest())
do {
let result = try await call.response.get()
// Successful creation returns ControlClient__ListCachesResponse
self.logger.debug("list caches received: \(result.cache)")
return CacheListSuccess(caches: result.cache)
return ListCachesResponse.success(ListCachesSuccess(caches: result.cache))
} catch let err as GRPCStatus {
return CacheListError(error: grpcStatusToSdkError(grpcStatus: err))
return ListCachesResponse.error(
ListCachesError(error: grpcStatusToSdkError(grpcStatus: err))
)
} catch let err as GRPCConnectionPoolError {
return CacheListError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
return ListCachesResponse.error(
ListCachesError(error: grpcStatusToSdkError(grpcStatus: err.makeGRPCStatus()))
)
} catch {
return CacheListError(error: UnknownError(message: "unknown cache create error \(error)"))
return ListCachesResponse.error(
ListCachesError(error: UnknownError(message: "unknown cache create error \(error)"))
)
}
}
}
Loading
Loading