How to parse json array with Retrofit and RxJava - kotlin

I have problem parsing array of objects using Retrofit + RxJava
JSON contains only this array
{
"files": [
{
"id": 10,
"notificationId": 15,
"name": "cats.ncs",
"dateTime": "2019-01-07T17:34:45"
}
]
}
retrotfit service
#GET("/api/FileApi/files")
fun files(): Observable<FilesResponse>
where FilesResponse is
data class FilesResponse(
#SerializedName("files")
var files: List<FileElement>
)
and FileElement
data class FileElement(
#SerializedName("id")
var id: Long,
#SerializedName("notificationId")
var notificationId: Long,
#SerializedName("name")
var name: String,
#SerializedName("dateTime")
var dateTime: String
)
when I run it I get always
the return type of CallObjectMethodA does not match
io.reactivex.Observable ApiService.files()
So how do I parse JSON containing only an array?

Try using RxJava2 Adapter
Integration
implementation 'com.squareup.retrofit2:adapter-rxjava2:{retrofit_version}'
Retrofit client setup
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //option 1
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread())) //option 2
.build();

Few notes
First of, you might need to show us how you call ApiService.files()
What could possibly went wrong
How it is called
What could be the possible solution
Invoke the method like this
ApiService
.files()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
// Handle the values here
},{
// Handle the error here
},{
// Handle the completion here (optional)
})
Not like this
ApiServices.files()
nor
var fileResponse = ApiServices.files()
Ensure the you have setup your retrofit builder well, as mentioned above
RxJava2CallAdapterFactory
GsonConverterFactory
Other than that, without the code on how you call it, we won't be able to help you in an in-depth manner.

Related

Upload Multiple Files With Ktor

I am Using ktor with kmm to upload a list of images to server
But there is no available guide in the docs to make me upload list of files
i am converting my files to byte array and uploading them
I tried to upload it this way
mainClient.post {
setBody(
MultiPartFormDataContent(
formData {
append("attachments[]", listOf(toBytes(kmmFile),toBytes(kmmFile)) )
}
)
)
}
but got connection refused
You can iterate through all byte arrays in a collection and call the append method for each of them. Here is an example:
val images: List<ByteArray> // assigned externally
val response = client.post("https://httpbin.org/post") {
setBody(MultiPartFormDataContent(
formData {
for (bytes in images) {
append("attachments[]", bytes)
}
}
))
}
I use below code for uploading single file and run forEach when call this method(for each n append doesn't work for me). I think your serve must be supported upload multiple file at same time.
override suspend fun upload(
uploadFiles: Map<String, File>,
texts: Map<String, String>
): ResultWrapper<ResponseData<List<UploadFileDto>>> {
return {
httpClient.submitForm {
url(BASE_URL + "api/v1/static/upload-file")
method = HttpMethod.Post
setBody(MultiPartFormDataContent(
formData {
headers {
append(
"document",
uploadFiles.entries.first().value.readBytes(),
Headers.build {
append(
HttpHeaders.ContentDisposition,
"filename=${uploadFiles.entries.first().value.name}"
)
})
}
}
))
}.body()
}

Ktor-server RequestValidation not working

I am using ktor (2.1.0) to create a small API. While doing so, I am trying to leverage Ktor-server cool functionalities, including RequestValidation.
That being said, it is not working and I can't figure out why since it looks to me very close to the examples in the documentation.
This is my server config:
embeddedServer(Netty, port = 8080) {
routing {
post("/stock") {
val dto = call.receive<CreateStockRequest>()
call.respond(HttpStatusCode.NoContent)
}
}
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
install(StatusPages) {
exception<RequestValidationException> { call, cause ->
call.respond(HttpStatusCode.BadRequest, cause.reasons.joinToString())
}
}
install(RequestValidation) {
validate<CreateStockRequest> { request ->
if (request.name.isBlank())
ValidationResult.Invalid("Stock must have a name")
if (request.symbol.isBlank())
ValidationResult.Invalid("Symbol must have a name")
ValidationResult.Valid
}
}
}
This is the request object being "received":
#Serializable
data class CreateStockRequest(val name: String, val symbol: String)
And this is the body being sent:
{
"name": "",
"symbol": "something"
}
I was expecting to get a BadRequest response, but I am getting a NoContent response as if everything was fine with the request.
Am I doing something wrong?
In your example, the server responds with BadRequest only if both "name" and "symbol" are blank. You can replace your validation logic with the when expression:
validate<CreateStockRequest> { request ->
when {
request.name.isBlank() -> ValidationResult.Invalid("Stock must have a name")
request.symbol.isBlank() -> ValidationResult.Invalid("Symbol must have a name")
else -> ValidationResult.Valid
}
}
I found the accepted answer to be a cleaner code solution, and that's why it is the accepted one.
That being said, the actual problem I was having is better explained by understanding that the compiler was getting confused with scopes.
On each "if" clause, I needed to return the validation result, but the compiler was not having it.
The reason for that was the fact that it got confused with the context to which my return statement referred.
My Solution was to use the '#' notation to specify the scope for the return:
validate<CreateStockRequest> { request ->
if (request.name.isBlank())
return#validate ValidationResult.Invalid("Stock must have a name")
if (request.symbol.isBlank())
return#validate ValidationResult.Invalid("Symbol must have a name")
return#validate ValidationResult.Valid
}
}

How to get the ressources address of a badge

I'm trying to build an Auction web application based on a scrypto smartcontract.
I have a register function that return a badge, from which I can build proof to call other methods that need authentication.
To build the proof, I need the address of the badge; using the pte-sdk, how would that be possible ?
I use :
const manifestRegistration = new ManifestBuilder()
//call the register function
.callMethod(auction.auctionId, "register", [])
//deposit the resource into my account
.callMethodWithAllResources(accountAddress, "deposit_batch")
.build()
.toString()
const receiptRegistration = await signTransaction(manifestRegistration);
console.log(receiptRegistrationt)
here is receipt :
{
"transactionHash": "b737899a3b78692d2ba49d83ccedeacd66f6168d107a2962828d621d6c73cb37",
"status": "Success",
"outputs": [
"{\"type\":\"Bucket\",\"value\":\"Bucket(1027u32)\"}",
"{\"type\":\"Unit\"}"
],
"logs": [],
"newPackages": [],
"newComponents": [],
"newResources": []
}
How do I know what is the resource in the returned bucket ?
Thank you
The first thing that pops into my head of solving this is returning the address as a string
pub fn register() -> (Bucket, String) -> {
...
(badge, badge.resource_address().to_string())
}
This should pop up in the outputs array

How to use Serde to parse a field that might fail to be deserialized without failing the entire deserialization?

I am deserializing some JSON objects which come in as requests. The input body is nested, but a certain field is sometimes misformatted for a variety of reasons. In that situation I still want the rest of the object. This doesn't all have to be done through serde; but what is happening now, is that if a single subfield is messed up, the whole request is trashed. I want to somehow still deserialize that result and just mark the field as errored out. How can this be done?
E.g. the data schema might look like:
struct BigNested {
a: Vec<A>,
b: B, // definition omitted
}
struct A {
keep_this: Foo,
trouble: SometimesBad,
}
trouble is the field that's frequently coming in messed up. I would be happy to (e.g.) turn trouble into a Result<SometimesBad, Whatever> and process it from there, but I don't know how to get serde to let me do that.
certain field is sometimes misformatted
You didn't say how malformed the incoming JSON was. Assuming it's still valid JSON, you can pull this off with Serde's struct flatten and customized deserialization:
The customized deserialization is done in a way that never fails for valid JSON input, although it may not return value of expected type if the input has unexpected format.
But these unexpected fields still need to go somewhere. Serde's struct flatten comes in handy here to catch them since any JSON snippet can be deserialized to a HashMap<String, Value>.
//# serde = { version = "1.0.103", features = ["derive"] }
//# serde_json = "1.0.44"
use serde::{Deserialize, Deserializer, de::DeserializeOwned};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Deserialize, Debug)]
struct A {
keep_this: Foo,
trouble: SometimesBad,
}
#[derive(Deserialize, Debug)]
struct Foo {
foo: i32,
}
#[derive(Deserialize, Debug)]
struct SometimesBad {
inner: TryParse<Bar>,
#[serde(flatten)]
blackhole: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
struct Bar {
bar: String,
}
#[derive(Debug)]
enum TryParse<T> {
Parsed(T),
Unparsed(Value),
NotPresent
}
impl<'de, T: DeserializeOwned> Deserialize<'de> for TryParse<T> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
match Option::<Value>::deserialize(deserializer)? {
None => Ok(TryParse::NotPresent),
Some(value) => match T::deserialize(&value) {
Ok(t) => Ok(TryParse::Parsed(t)),
Err(_) => Ok(TryParse::Unparsed(value)),
},
}
}
}
fn main() {
let valid = r#"{ "keep_this": { "foo": 1 }, "trouble": { "inner": { "bar": "one"}}}"#;
println!("{:#?}", serde_json::from_str::<A>(valid));
let extra_field = r#"{ "keep_this": { "foo": 1 }, "trouble": { "inner": { "bar": "one"}, "extra": 2019}}"#;
println!("{:#?}", serde_json::from_str::<A>(extra_field));
let wrong_type = r#"{ "keep_this": { "foo": 1 }, "trouble": { "inner": { "bar": 1}}}"#;
println!("{:#?}", serde_json::from_str::<A>(wrong_type));
let missing_field = r#"{ "keep_this": { "foo": 1 }, "trouble": { "inner": { "baz": "one"}}}"#;
println!("{:#?}", serde_json::from_str::<A>(missing_field));
let missing_inner = r#"{ "keep_this": { "foo": 1 }, "trouble": { "whatever": { "bar": "one"}}}"#;
println!("{:#?}", serde_json::from_str::<A>(missing_inner));
}
(The credit isn't all mine. Serde's issue 1583 basically has everything.)

RxJava Flowable.Interval backpressure when flatmap with single

I'm having a scenario where I need to periodically call an API to check for a result. I'm using Flowable.interval to create an interval function which calls the API.
However, I'm having trouble with backpressure. In my example below, a new single is created on each tick in the interval. The desired effect is to only call the API if a call is not already in progress
Flowable.interval(1, 1, TimeUnit.SECONDS).flatMap {
System.out.println("Delay $it")
//simulates API call
Single.just(1L).doAfterSuccess {
System.out.println("NEW SINGLE!!!")
}.delay(4, TimeUnit.SECONDS).doAfterSuccess {
System.out.println("SINGLE SUCCESS!!!")
}.toFlowable()
}.subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).blockingFirst()
I can solve this using a filter variable like so:
var filter = true
Flowable.interval(1, 1, TimeUnit.SECONDS).filter {
filter
}.flatMap {
System.out.println("Delay $it")
Single.just(1L).doOnSubscribe {
filter = true
}.doAfterSuccess {
System.out.println("NEW SINGLE!!!")
}.delay(4, TimeUnit.SECONDS).doAfterSuccess {
System.out.println("SINGLE!!!")
filter = true
}.toFlowable()
}.subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).blockingFirst()
But it seems like a hacky solution. I've tired applying onBackPressureDrop after the interval function, but it has no effect.
Any suggestions?
You have to constrain flatMap as well:
Flowable.interval(1, 1, TimeUnit.SECONDS)
.onBackpressureDrop()
.flatMapSingle({
System.out.println("Delay $it")
//simulates API call
Single.just(1L).doAfterSuccess {
System.out.println("NEW SINGLE!!!")
}.delay(4, TimeUnit.SECONDS).doAfterSuccess {
System.out.println("SINGLE SUCCESS!!!")
}
}, false, 1) // <----------------------------------------------------------
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe()