Im implementing and designing a workflow that performs both HTTP and BLE requests.
Im having trouble figuring out the best approach to design a service to handle both types of requests, and manage state.
I thought to make a base class Request
with an execute
method that all types of requests will conform to.
BLE and HTTP are quite different and dont share any attributes, so I made an enum to handle their associated values. It includes a provider
which is a client that exposes api to perform actual BLE
or HTTP
request.
enum RequestCode: UInt {
case getStatus = 0x01
case download = 0x02
case confirm = 0x03
case cancel = 0x04
case getAuthToken = 0xF5
}
enum RequestType {
case BLE(code:RequestCode, provider:BLEProvider, characteristic: CBCharacteristic, service: CBService)
case HTTP(code:RequestCode, provider:NetworkProvider, data: Data, accessToken:String, method:HTTPMethod, params:[String:Any]?)
}
class Request {
var type: RequestType
init(type: RequestType) {
self.type = type
}
func execute() {
switch type {
case .BLE(let code, let provider, let characteristic, let service):
provider.writeValue(data: code.rawValue, characteristic: characteristic, service:service)
case .HTTP(let code, let provider, let data, let accessToken, let method, let params):
provider.performRequest(data: data, token: accessToken, method: method, params:params)
}
}
}
Questions
What is a better way to structure my requests, to handle both BLE and HTTP?
How can I handle transitions from one state/event to the next? Does it make sense to try to use promises, where I can call
execute
on each request, and chain them all together? If not, should I think of adding each request to a queue, if and how could a state machine be designed aroundRequestCode
which are the events in the sequence?