Skip to content

Instantly share code, notes, and snippets.

@radityagumay
Created September 10, 2020 15:57
Show Gist options
  • Save radityagumay/4dee01c302bcf4bc585fd28491cefdd5 to your computer and use it in GitHub Desktop.
Save radityagumay/4dee01c302bcf4bc585fd28491cefdd5 to your computer and use it in GitHub Desktop.
Network Builder in Swift based on Android Retrofit Builder Pattern
protocol Dao {
func insert(key: String, value: Any) -> Bool
func find(key: String) -> Any?
}
protocol StoreAdapterFactory {
func create() -> Dao
}
class StoreMemoryAdapter : Dao {
private var maps = [String: Any]()
private init() { }
class Factory : StoreAdapterFactory {
func create() -> Dao {
return StoreMemoryAdapter()
}
}
func insert(key: String, value: Any) -> Bool {
maps[key] = value
return true
}
func find(key: String) -> Any? {
return maps[key]
}
}
class Network {
private var dao: Dao
private(set) var service: Service
init(
dao: Dao,
service: Service
) {
self.dao = dao
self.service = service
}
}
class Service {
private var networkInterceptors = [NetworkInterceptor]()
private var applicationInterceptors = [ApplicationInterceptor]()
init(
networkInterceptors: [NetworkInterceptor],
applicationInterceptors: [ApplicationInterceptor]
) {
self.networkInterceptors = networkInterceptors
self.applicationInterceptors = applicationInterceptors
}
func request(request: Request) -> Response {
for networkInterceptor in networkInterceptors {
networkInterceptor.chain(request: request)
}
for applicationInterceptor in applicationInterceptors {
applicationInterceptor.chain(request: request)
}
if request.method == Method.GET {
return Response(message: "success", code: 200)
}
return Response(message: "error", code: 404)
}
}
struct Request {
let method: Method
let body: String
}
struct Response {
var message: String
var code: Int
}
enum Method {
case POST
case GET
case PUT
case DELETE
case PATCH
}
protocol NetworkInterceptor {
func chain(request: Request) -> Response
}
protocol ApplicationInterceptor {
func chain(request: Request)
}
class LogInterceptor : ApplicationInterceptor {
func chain(request: Request) {
print("Log \(request.body)")
print("Log \(request.method)")
}
}
class EncyrptionInterceptor : NetworkInterceptor {
func chain(request: Request) -> Response {
print("request is encrypted")
print(request)
return Response(message: "response is encrypted", code: 200)
}
}
class NetworkSDK {
private init() { }
class Builder {
private(set) var storeAdapterFactory: StoreAdapterFactory?
private(set) var networkInterceptors = [NetworkInterceptor]()
private(set) var applicationInterceptors = [ApplicationInterceptor]()
func setStoreAdapterFactory(adapter: StoreAdapterFactory) -> Builder {
self.storeAdapterFactory = adapter
return self
}
func addNetworkInterceptor(interceptor: NetworkInterceptor) -> Builder {
self.networkInterceptors.append(interceptor)
return self
}
func addApplicationInterceptor(interceptor: ApplicationInterceptor) -> Builder {
self.applicationInterceptors.append(interceptor)
return self
}
func build() -> Network {
assert(storeAdapterFactory != nil)
return Network(
dao: storeAdapterFactory!.create(),
service: Service(
networkInterceptors: networkInterceptors,
applicationInterceptors: applicationInterceptors
)
)
}
}
}
let network = NetworkSDK.Builder()
.setStoreAdapterFactory(adapter: StoreMemoryAdapter.Factory())
.addNetworkInterceptor(interceptor: EncyrptionInterceptor())
.addApplicationInterceptor(interceptor: LogInterceptor())
.build()
let response = network.service.request(request: Request(method: .GET, body: "hello world"))
print(response)
let response2 = network.service.request(request: Request(method: .DELETE, body: "keanu"))
print(response2)
@radityagumay
Copy link
Author

Screen Shot 2020-09-10 at 22 53 13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment