DRF: Serializing data from external network request - json

I'm using Django Rest Framework and making external network request which data isn't needed in models but pure serializers.
I make an external request to get some data from a server which returns JSON. Here is a quick snipped of it.
{
"request": [
{
"packages": {
"gold": [
{
"name": "Gold Package 1",
"value": "Gold1"
},
{
"name": "Gold Package 2",
"value": "Gold2"
}
],
"bronze": [
{
"name": "Bronze package 1",
"value": "Bronze1"
},
{
"name": "Bronze Package 2",
"value": "Bronze2"
}
]
}
]
}
After getting this request, I want to return data within this format.
How can this be achieved?
"response": [{
"details": {
"legacy_packages": [{
"name": "Gold Package 1",
},
{
"name": "Gold Package 2",
}
]
}
},

Do you really need serializer for this? Looks like plain code will be ok. Assuming request variable to be the first dictionary:
response = {
'response': [{
"details": {
"legacy_packages": [
{
"name": package['name'],
}
for package in request['request'][0]['packages']['gold']
]
}
}]
}
Introduce intermediate variables if needed for clarity and cleaner code.

Related

graphql, nesjs and prisma api response solution need

i use nestjs, graphQL and prisma module to create my endpoint and web service ;
i am looking for way to create custom response in graphQL my endpoint is simple
select books from database my response is
{
"data": {
"books": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}
}
but in need something like this
{
"result": "success",
"msg" : "list ...",
"data": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}

I need assistance in building my own json schema

How do I build my own json schema to validate the json coming back from an api is the same structure? I have this sample JSON
{
"httpStatus": 200,
"httpStatusMessage": "success",
"timestamp": "2020-11-11T19:32:45",
"response": {
"header": {
"SchoolId": 10006,
"SchoolName": "Naples"
},
"body": {
"dataProviders": [
{
"dataProviderId": 14,
"students": [
{
"studentId": 1000611000,
"driverGrade": "Junior",
"firstName": "Authur",
"lastName": "Boccuto"
},
{
"studentId": 1000611001,
"studentGrade": "Senior",
"firstName": "Antwan",
"lastName": "Carter"
}
]
}
]
}
}
}
At times it can come in with a different structure and I need to build my own json schema to validate that it's the same before manipulating the json data. How do I build my own schema to make sure that it has a valid structure?

Using a template variable that contains a JSON object with Postman Collection Runner

I am attempting to use the Postman collection runner to post several objects to an API from a JSON file structured something like this:
[
{
"id": "170",
"body": {
"name": "prod1",
"category": "category1"
},
"anotherProperty": "xyz"
},
{
"id": "171",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "dfg"
},
{
"id": "172",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "abc"
}
]
My problem seems to be with the body since it is an object:
Here is what I have in the Body > raw application/json of the request that my collection is using:
{
"$id": "{{id}}",
"body": {{body}},
"anotherProperty": "{{anotherProperty}}"
}
When viewing what it is plugging in it looks like:
{
"id": "170",
"body": {[object Object]}, // instead of the actual object
"anotherProperty": "xyz"
}
I needed to add the following to the Pre-request script:
let properties = pm.iterationData.get('properties');
pm.variables.set('properties', JSON.stringify(properties));
let body = pm.iterationData.get('body');
pm.variables.set('body', JSON.stringify(body));
And the raw JSON is plugged in with no problem!

Problems with v1/queryContext needed for SpagoBI integration

In version 1.7.0 of Orion CB running the docker version in Docker for Windows,
if I create a simple object doing POST http://localhost:1026/v1/updateContext
with the body:
{
"contextElements": [
{
"type": "Car",
"id": "myNewCar",
"attributes": [
{
"name": "maxSpeed",
"type": "integer",
"value": "220"
}
]
}
],
"updateAction": "APPEND"
}
I get the answer:
{
"contextResponses": [
{
"contextElement": {
"type": "Car",
"isPattern": "false",
"id": "myNewCar",
"attributes": [
{
"name": "maxSpeed",
"type": "integer",
"value": ""
}
]
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}
}
]
}
Then, if I do POST http://localhost:1026/v1/queryContext with the same headers and the same components with the body
{
"entities": [
{
"type": "Car",
"isPattern": "false",
"id": "myNewCar"
}
]
}
I get the following:
{
"errorCode": {
"code": "404",
"reasonPhrase": "No context element found"
}
}
Which shouldn't be problematic (I can query the entities with v2 API, for instance) if it wasn't needed for integration with data representation tools such as SpagoBI as documented in http://spagobi.readthedocs.io/en/latest/user/NGSI/README/
What can I do? I am doing something wrong with the context provision?
Thanks!
My problem was that I was using a imported Postman collection of the API (Downloaded from https://github.com/telefonicaid/fiware-orion/tree/develop/doc/apiary/v2) and accidentally I was using the header Fiware-Service.
You are right and your tests work properly.
Thanks for the prompt reply!!

How to update object in nested arrays in mongo db?

Assuming I have the following document structure:
{
"name": "myProduct",
"perspectives" : [
{
"name": "p1",
"views" : [
{
"name": "v1"
},
{
"name": "v2"
}
]
},
{
"name": "p2",
"views" : [
{
"name": "v1"
},
{
"name": "v2"
}
]
}
]
}
How would I go about updating the document structure to add an "alias" field to each of the views?
Basically I'm looking to do something like perspectives.views.alias: "av1" for all perspectives.views.name: "v1".
The resulting structure would look like this:
{
"name": "myProduct",
"perspectives" : [
{
"name": "p1",
"views" : [
{
"name": "v1",
"alias": "av1"
},
{
"name": "v2",
"alias": "av2"
}
]
},
{
"name": "p2",
"views" : [
{
"name": "v1",
"alias": "av1"
},
{
"name": "v2",
"alias": "av2"
}
]
}
]
}
To clarify, I'd like to do something like this:
foreach (view in product.perspectives.views)
{
if (view.name == "p1")
view.add("alias", "av1");
}
You'll have to loop though your documents, the perspectives in each document and the views in each perspective. Then update the views and save the updated document. Something like this should do the trick:
db.products.find().forEach(function (product) {
product.perspectives.forEach(function (perspective) {
perspective.views.forEach(function (view) {
view.alias = "a" + view.name;
})
});
db.products.save(product);
})
You can paste this function into the Mongo shell and run it from there. As an alternative, you can save it in a file called updateProducts.js and run the file with the Mongo shell:
mongo nameOfDatabase updateProducts.js
The function will be executed on the server, so it should be quite fast.