Closed DrikABrak closed 11 months ago
Is the content expected to be base64 encoded?
Either way, this appears to be an unhelpful error sent by the Java side.
Hi @Lukasa, Thanks for your answer. Yes the content should be base64 encoded. It's image files. I don't understand this error but it doesn't work.
But if my code seems to be correct or not? I have a doubt on oneof
structure.
Thanks
Oh yeah, good catch I didn't notice that. You should only set one of those. It's hard to tell from the API but you probably have to send those in a specific order
What did youmean by 'You should only set one of those' ? I have an old code which use Combine and seems to work but i'd like to remove Combine. The only difference is that in old cold, data is sent by chunks but i don't think that it is my problem.
let coldObs = datas.enumerated().publisher
.buffer(size: 20, prefetch: .keepFull, whenFull: .dropOldest)
.flatMap(maxPublishers: .max(1)) { (index, pageData) -> AnyPublisher<Authenticate_SendDocumentInfos, Error> in
let base64EncodedData = pageData.base64EncodedData()
let chunks = base64EncodedData.chunk(chunkSize: 1000).map{ chunk in
Authenticate_SendDocumentInfos.with { $0.content = chunk}
}
let tmp = [
Authenticate_SendDocumentInfos.with {$0.part = Int32(index + 1) },
Authenticate_SendDocumentInfos.with { $0.contentType = mimeTypes[safe:index] ?? mimeTypes[0]},
] + chunks
return tmp.publisher.setFailureType(to: Error.self).eraseToAnyPublisher()
}
.prepend(Authenticate_SendDocumentInfos.with { $0.type = documentType })
.prepend(Authenticate_SendDocumentInfos.with { $0.title = documentName })
.eraseToAnyPublisher()
So my problem is finally resolved! Like @Lukasa said before, we should send each parameter one by one in a specific order.
Here is the right code:
/// Send a document
func sendDocument(docs: [Data]) -> Authenticate_SendDocumentStatus? {
let result: Authenticate_SendDocumentStatus
var logger = Logger(label: "io.grpc")
logger.logLevel = .trace
let callOptions = CallOptions(customMetadata: GRPCClientService.shared.callOptions!.customMetadata, logger: logger)
let stream = getAuthenticateClient().sendDocument(callOptions: callOptions)
var docRequest: Authenticate_SendDocumentInfos = .with { $0.title = "driving_licence_label".localize() }
_ = stream.sendMessage(docRequest)
docRequest = .with { $0.type = .drivingLicense }
_ = stream.sendMessage(docRequest)
var part = 1
for doc in docs {
docRequest = .with { $0.part = Int32(part) }
_ = stream.sendMessage(docRequest)
docRequest = .with { $0.contentType = doc.mimeType }
_ = stream.sendMessage(docRequest)
docRequest = .with { $0.content = doc.base64EncodedData() }
_ = stream.sendMessage(docRequest)
part += 1
}
_ = stream.sendEnd()
do {
result = try stream.response.wait()
} catch {
print("RPC method ‘sendDocument’ failed: \(error)")
return nil
}
return result
}
Thanks.
What are you trying to achieve?
Hi, i'm new in grpc-swift and i would like to send several documents (data) but i have an error. Is it a good way to do it or i missing something ? Here is my proto (with oneof for SendDocumentInfos):
What have you tried so far?
And here is my function to send documents that i try:
And here is my log:
Thanks in advance for all your advices and solutions.