Python Fiware Orion Context Broker problems - python-2.7

I can't create an entity.
Payload:
datos = {
"id": "1",
"type": "Car"
}
Query:
jsonData = json.dumps(datos)
url = 'http://130.206.113.177:1026/v2/entities'
response = requests.post(url, data=jsonData, headers=head)
Error:
'{"error":"BadRequest","description":"attribute must be a JSON object, unless keyValues option is used"}'

Did you define head object? I don't see it defined in the code provided by you.
I have the intuition that you forgot to define the header 'Content-Type' which must be defined with the value:
"Content-Type": "application/json"
On the other hand, defining the headers in the following way works perfectly for me, even using the Orion instance that you pointed out in the description of your question.
import json
import requests
head = {"Content-Type": "application/json"}
datos = { "id": "1", "type": "Car"}
jsonData = json.dumps(datos)
url = 'http://130.206.113.177:1026/v2/entities'
response = requests.post(url, data=jsonData, headers=head)
print response
Note that if you invoke your example as it is, you will probably return an error HTTP 422, because the object already exists (the one that I created during the test).
Regards!

Related

How to query from AppSynch using e.g. Urllib3

I have create an AppSynch API on AWS. I can easily query it using the console or some AWS specific package. However I would want to query it using a simple package such as e.g. urllib3. Its surprisingly hard to find anyone doing using a direct api call (everyone uses some kind of aws related packages or solutions that i cant seem to get to work). The query I want to do is:
mutation provision {
provision(
noteId:
{ec2Instance: "t2.micro",
s3Bucket: "dev"})}
I have tried with different variations of:
query = """
mutation provision {
provision(
noteId:
{ec2Instance: "t2.micro",
s3Bucket: "dev"})}
"""
headers = {"x-api-key": api_key}
http = urllib3.PoolManager()
data = json.dumps("query": query, "variables": {}, "operationName": "somename")
r = http.request('POST', url, headers=headers,
data=data.encode('utf8'))
But I somehow cannot get it to work, i keep on getting messages that the API cant understand the POST request
I found the solution:
import requests
import json
APPSYNC_API_KEY = APPSYNC_API_KEY
APPSYNC_API_ENDPOINT_URL = APPSYNC_API_ENDPOINT_URL
headers = {
'Content-Type': "application/graphql",
'x-api-key': APPSYNC_API_KEY,
'cache-control': "no-cache",
}
query = """
mutation provision {
provision(
inputParameters:
{ec2Instance: "t2.micro",
s3Bucket: "dev"})}
"""
payload_obj = {"query": query}
payload = json.dumps(payload_obj)
response = requests.request("POST", APPSYNC_API_ENDPOINT_URL, data=payload, headers=headers)
print(response)

Postman Variables

I am trying to parse "Id" from below json to store it in a variable
{
"cf35864e-944d-11e9-8aff-22000ab8d684": {
"Id": "1a45b704-944e-11e9-8aff-22000ab8d684",
"Name": "Test plan",
"Type": "Limited",
"Credits": 10119.70,
"AvailableCredits": 500.100
}
}
I've tried
bodydata =JSON.parse(responseBody)
planid=bodydata.cf35864e-944d-11e9-8aff-22000ab8d684.Id
console.log(planid)
But postman throws errors, is there any way so that only ID can be fetched.
This should work: planid=bodydata["cf35864e-944d-11e9-8aff-22000ab8d684"]['Id']
As per your mentioned JSON data, the below may work.
let resp = pm.response.json();
console.log(resp.cf35864e-944d-11e9-8aff-22000ab8d684.Id);
Above will work only if "cf35864e-944d-11e9-8aff-22000ab8d684" value is static and only one object is returned.

Formatting for Firehose transformation output

I am using AWS Kinesis Firehose with a custom Data Transformation. The Lambda's written in Python 3.6 and returns strings that look like the following:
{
"records": [
{
"recordId": "...",
"result": "Ok",
"data": "..."
},
{
"recordId": "...",
"result": "Ok",
"data": "..."
},
{
"recordId": "...",
"result": "Ok",
"data": "..."
}
]
}
This Lambda is perfectly happy, and logs outputs that look like the above just before returning them to Firehose. However, the Firehose's S3 Logs then show an error:
Invalid output structure: Please check your function and make sure the processed records contain valid result status of Dropped, Ok, or ProcessingFailed.
Looking at the examples for this spread across the web in JS and Java, it's not clear to me what I need to be doing differently; I'm quite confused.
If your data is a json object, you can try following
import base64
import json
def lambda_handler(event, context):
output = []
for record in event['records']:
# your own business logic.
json_object = {...}
output_record = {
'recordId': record['recordId'],
'result': 'Ok',
'data': base64.b64encode(json.dumps(json_object).encode('utf-8')).decode('utf-8')
}
output.append(output_record)
return {'records': output}
base64.b64encode function only works with b'xxx' string while 'data' attribute of output_record needs a normal 'xxx' string.
I've found the same error using Node.js.
Reading the documentation http://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html my mistake was not base64-encoding of the data field of every record.
I resolved doing this:
{
recordId: record.recordId,
result: 'Ok',
data: new Buffer(JSON.stringify(data)).toString('base64')
}
You can check the code in my repo.
https://github.com/hixichen/golang_lamda_decode_protobuf_firehose

I want to manipulate the file in MarkLogic using Python

declareUpdate();
//get Docs
myDoc = cts.doc("/heal/scripts/Test.json").toObject();
//add Data
myDoc.prescribedPlayer =
[
{
"default": "http://www.youtube.com/watch?vu003dhYB0mn5zh2c"
}
]
//persist
xdmp.documentInsert("/heal/scripts/Test.json",myDoc,null,"scripts")
You're looking to add a new JSON property. You can do that using a REST Client API request, sending a PATCH command. Use an insert instruction in the patch.
See the note in Specifying Position in JSON, which indicates that
You cannot use last-child to insert a property as an immediate child of the root node of a document. Use before or after instead. For details, see Limitations of JSON Path Expressions.
Instead, your patch will look something like:
{
"insert": {
"context": "/topProperty",
"position": "after",
"content":
[
{
"default": "http://www.youtube.com/watch?vu003dhYB0mn5zh2c"
}
],
}
}
where topProperty is a JSON property that is part of the root node of the JavaScript object you want to update.
If that approach is problematic (for instance, if there is no topProperty that's reliably available), you could also do a sequence of operations:
retrieve the document
edit the content in Python
update the document in the database
With this approach, there is the possibility that some other process may update the document while you're working on it. You can either rely on optimistic locking or a multi-statement transaction to work around that, depending on the potential consequences of someone else doing a write.
Hey #Ankur Please check below python method,
def PartialUpdateData(self,filename, content, context):
self.querystring = {"uri": "/" + self.collection + "/" + filename}
url = self.baseUri
self.header = {'Content-Type': "application/json"}
mydata = {
"patch":[{ "insert": {
"context": context,
"position": "before",
"content": content
}}]}
resp = requests.patch(url + "/documents", data=json.dumps(mydata),
headers=self.header, auth=self.auth, params=self.querystring)
return resp.content
I hope this can solve your problem.

Passing JSON data from response to request in Django

I have a Django (1.8.3) view that:
Makes a GET request to Server A (jetty), which returns JSON data in the body of the response. Then,
Makes a POST to Server B (node.js), passing the JSON data recieved from Server A in the body of the request.
The JSON data is structured like:
{
name: "foo",
details: {
"date": "today",
"isCool": "no",
},
stuff: [
{
"id": "1234",
"rating": "5",
}, {
"id": "5678",
"rating": "1",
},
]
}
But I can't figure out how to get the JSON from Server A's response into the request to Server B in my Django view. If I do this:
jetty_response = requests.request(method='GET', url=jetty_url)
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.json())
I get the JSON object in Server B, but it looks like this:
{
name: "foo",
details: [ "date", "isCool"],
stuff: [ "id", "rating", "id", "rating"]
i.e. the name property is correct, but the details dict is instead received as the keyset of the original dict, and the stuff list is received as a flat array of the keysets in all objects in the original dict.
If I instead do this in django:
node_response = requests.request(method="POST", url=node_url,
data=json.dumps(jetty_response.json()))
I get an empty object in node, and same goes if I do simply:
data=jetty_response.content
How do I make this request??
Figured it out myself.
As is usually the case, the simplest answer:
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.content)
worked fine once I took a closer look at my log and realized my POSTs were bouncing back 413, and then adjusted the size limit on my bodyParser in express.