Skip to content

Instantly share code, notes, and snippets.

@mkows
Created September 19, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkows/1c9f67cf5e3fa340a969e3da51f8a935 to your computer and use it in GitHub Desktop.
Save mkows/1c9f67cf5e3fa340a969e3da51f8a935 to your computer and use it in GitHub Desktop.
Send entries as JSON to API via POST
# Send file entries (each per line) via POST.
#
# Assumes that:
# - api consumes entries asynchronously,
# - it takes api 1s to process each entry (hence waits 25+5s after each page of 25)
#
# Replacement for:
#
# curl \
# -X POST \
# -d '["1eb249f4-6630-43cf-a8c1-7cae504d090c", "1eb249f4-6630-43cf-a8c1-7cae504d090d", "1eb249f4-6630-43cf-a8c1-7cae504d090e", ..., ..., ... ]' \
# -H "Content-Type: application/json" \
# "http://localhost:9099/entries"
#
# Usage:
#
# pip install requests
# python <this-file>
import requests
import json
import time
input_file = 'file.txt'
endpoint_url = 'http://localhost:9099/entries'
f = open(input_file, 'r')
all_entries = f.read().splitlines()
f.close()
page_size = 25
def go(lines, page_num, total=0):
page = lines[0:page_size]
curr_page_size = len(page)
if (curr_page_size > 0):
print("will call with " + str(curr_page_size) + " users")
requests.post(endpoint_url, json=page)
curr_total = total + curr_page_size
print("so far pushed: " + str(curr_total) + ". still to go: " + str(len(lines) - curr_page_size))
time.sleep(page_size + 5)
remaining = lines[page_size:]
go(remaining, page_num+1, curr_total)
else:
print("all done on page " + str(page_num) + ". handled in total: " + str(total))
go(all_entries, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment