How to handle JSON array response while using APIConsumer? - chainlink

Using the APIConsumer contract we can feed data from API to the smart contract.
Eg: If the server response is:
{
"RAW":{"ETH":{"USD":{"VOLUME24HOUR": 10000,}}}
}
Then, data can be obtained as:
request.add("get", URL);
request.add("path", "RAW.ETH.USD.VOLUME24HOUR");
Similarly, If the server response contains some JSON array,
Eg:
{
"date":"530934083405834",
"results": [
{
"id": 9865,
"rank":1
},
{
"id": 9869,
"rank": 2
},
{
"id": 9866,
"rank": 3
}
]}
Then in this case is there a way to get the id of the rank 1 i.e results[0]["id"]?

To get results[0]["id"] your path in the request needs to be
request.add("path", "results.0.id");

Related

How to mention array of object in hasura graphql action type?

How to define the Action type in Hasura graphql(console) for below given API response?
{
"data": [
{
"name": "Jordan Smith",
"id": 101,
"location": "Florida",
"speciality_id": 214
},
{
"name": "Cathy Morphy",
"id": 104,
"location": "london",
"speciality_id": 214
}
],
"count": 2 }
If you have control over what's in your API response, and can change it to just return an array of objects (what's inside data), then something like this should work:
type Query {
doctors(specialty_id: Int, search: String, limit: Int, offset: Int): [Doctor]
}
type Doctor {
name: String
id: Int
location: String
specialty_id: Int
}
If you don't have control over the API response, you'll need another layer (e.g. a serverless function or another server endpoint to request the data and transform the response into the format Hasura Actions can handle).
See also:
https://github.com/hasura/graphql-engine/issues/4806
https://hasura.io/docs/latest/graphql/core/actions/types/index.html#custom-graphql-types

How to prepare the JSON payload from the response in JMeter

I am getting response from one API and need to prepare the payload from that response.
For example the response as like
{
"data": {
"total_count": 5,
"userIds": [1,2,3,4,5]
}
Need to make the payload from the response to other API like
{
"users": [
{
"user_id": 1,
"invite_amount": 100,
},
{
"user_id": 2,
"invite_amount": 100
},
{
"user_id": 3,
"invite_amount": 100
},
{
"user_id": 4,
"invite_amount": 100
},
{
"user_id": 5,
"invite_amount": 100
}
]
}
Above payload need to send to the another API
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def userIds = new groovy.json.JsonSlurper().parse(prev.getResponseData()).data.userIds
def payload = [:]
def users = []
userIds.each { userId ->
def user = [:]
user.put('user_id', userId)
user.put('invite_amount', 100)
users.add(user)
}
payload.put('users', users)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
That's it, you will be able to refer the generated payload value as ${payload} where required
More information:
JsonSlurper and JsonBuilder
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
Have a look at this link
Basically you need to use JMeter JSON Extractor

How to use the "nextToken" parameter to test Pagination?

I am working with a sample of graphql query which I want to further bind to a FlatList object in a React-Native app. But I am still struggling to understand what I should pass on then nextToken parameter in order to get a slice of objects destined to a second page...
Tried to pass the next or last id or index, but it didn't work - it asks me to provide a valid nextToken, which I don't know what kind of data it is.
I'm running through AppSynch console.
My query:
query ListResources(
$nextTokenPlants: String = "Orange Tree"
$limitPlants: Int = 3
) {
listResources {
items {
id
name
Plants(limit: $limitPlants, nextToken:$nextTokenPlants) {
items {
id
name
filterName
description
bath
tea
insence
children
}
nextToken
}
}
nextToken
}
}
This is the result I get:
{
"data": {
"listResources": {
"items": [
{
"id": "361dee16-d567-41ed-b1d4-9baa4a7ffdcc",
"name": "Plantas",
"Plants": null
}
],
"nextToken": null
}
},
"errors": [
{
"path": [
"listResources",
"items",
0,
"Plants"
],
"data": null,
"errorType": "DynamoDB:UserIllegalArgumentException",
"errorInfo": null,
"locations": [
{
"line": 9,
"column": 7,
"sourceName": null
}
],
"message": "com.amazonaws.deepdish.common.pagination.InvalidPaginationTokenException: Invalid pagination token given."
}
]
}
I expected to get a list of 3 of the itens stored on "Plants", starting from the "Orange Tree" onwards... Could anyone pls shed some light on it?
nextToken is a String that you get in the response when you send a request, looks like
eyJ2ZXJzaW9uIjoxLCJ0b2tlbiI6IkFRSUNBSGdtYVZObHlaR3FSa3hDRnFVWWdFeEZDM0FMY1JRdE9UOEt2dWFLMExzcjJ3RlBPMGdpcC96bjQ5VjFOVElKUng1M0FBQUNlVENDQW5VR0NTcUdTSWIzRFFFSEJxQ0NBbVl3Z2d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hQWVRuMGRQNU5aWG5pODU1eGZyL1N2dDQ2VU0xb3I2UEE9PSJ9
and it has nothing to do with your table keys.
Let me explain now how does it work:
When you send the request for the first time, you don't send nextToken since you don't have it and then you get the first portion of your data.
When you receive that first portion, if there exist more data, you get nextToken in the response.
Now, you have nextToken, you send another request to get the 2nd portion of your data. You should send that token in the request, so your backend knows from where to start.
You get your 2nd portion of data and a new nextToken if there is still some data to fetch (then you need to repeat step 2) or null in nextToken if there is no more data.
I hope that it's clear now

laravel/codeception : test if json response contains only certain keys

I have a json array coming from my api as response:
{
"data": [
{
"id": 1,
"name": "abc"
}
}
I am using laravel for api and laravel-codeception for testing.
public function getAll(ApiTester $I)
{
$I->sendGET($this->endpoint);
}
I have to test if the response contains only id and name key (not any other key) example this response should fail the test.
{
"data": [
{
"id": 1,
"name": "abc",
"email":"abc#xyz"
}
}
I have found $I->seeResponseContainsJson(), but it checks if JSON is present or not. It does not check if JSON response contains only specified keys.
Thanks.

Wrapping Data Structure in a data key API Blueprint / Apiary

So let's say I have a 200 response which body should be:
{
"data": [
{
"id": 1,
"title": "Activity 1"
},
{
"id": 1,
"title": "Activity 2"
}
]
}
I have managed to get this behavior of the response body by using this in API Blueprint.
+ Response 200 (application/json)
+ Attributes
+ data (array[Activity])
(Note that I can't add the data key to the data structure itself, because it's only present on the single response. If I need to nest an Activity inside another structure, it shouldn't have the data key.)
This doesn't seem right
The reason why I don't think it's the correct way of doing it, is beacuse of the JSON schema for this response which is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"type": "array"
}
}
}
Note how the actual activity is excluded.
How can I wrap my response in a data key properly, and have it reflected in both the body AND the schema?
You should use this line:
+ data(array[Activity], fixed-type)
The fixed-type keyword fixes the type of the items in the array.

Resources