loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import json
import demjson

url = "https://scalr.api.appbase.io/newstreamingapp/books/_bulk"

# Array of data objects
dataArray = [
  {
    "id": "X1",
    "department_name": "Books",
    "department_name_analyzed": "Books",
    "department_id": 1,
    "name": "A Fake Book on Network Routing",
    "price": 1032
  },
  {
    "id": "X2",
    "department_name": "Books",
    "department_name_analyzed": "Books",
    "department_id": 2,
    "name": "A Fake Book on Computer Science",
    "price": 4125
  },
  {
    "id": "X3",
    "department_name": "Books",
    "department_name_analyzed": "Books",
    "department_id": 3,
    "name": "A Fake Book on Reactive Programming",
    "price": 3846
  }
]

payload = ""

# Creating request body
for data in dataArray:
  # Pushing the operation and _id for each request
  type = {
    "index": {
      "_id": data['id']
    }
  }
  payload += demjson.encode(type) + "\n" + demjson.encode(data) + "\n"

print payload

headers = {
  'Authorization': "Basic bWVxUmY4S0pDOjY1Y2MxNjFhLTIyYWQtNDBjNS1hYWFmLTVjMDgyZDVkY2ZkYQ==",
  'Content-Type': "application/x-ndjson"
}

# Bulk request including the index method and all data objects
response = requests.request("POST", url, data=payload, headers=headers)

parsed = json.loads(response.text)
print json.dumps(parsed, indent=4, sort_keys=True)