Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rexlx committed Feb 20, 2024
1 parent e252131 commit a25f6c5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion example/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,40 @@
import time

def get_data(i, url):
"""perform a GET request to the given URL and return the response as a dictionary
This is only to demonstrate "doing something" so we can send some stats to the statwriter
"""

# the response dictionary
res = {
"value": [],
"time": None,
"id": None,
"extra": []
}

# perform the GET request
response = requests.get(url)

# try to parse the response and fill the dictionary
try:
# this is the time in ISO 8601 format + Z
t = dt.now().strftime("%Y-%m-%dT%H:%M:%S")
t += "Z"

data = response.json()

# i happen to know in this case that the response is a dictionary with a key 'data'
out = data['data']

elapsed = response.elapsed.total_seconds()

res['value'] = [elapsed]
res['time'] = t
res['id'] = out
res['extra'] = [{"iteration": i, "url": url}]
except Exception as e:
# dont blow up if something goes wrong, just inform the user
print(e)
return res

Expand All @@ -33,7 +49,7 @@ def post_to_statwriter(data, url):
def main():
resource_uri = "https://namer.nullferatu.com"
statwriter_uri = "http://cobra.nullferatu.com:20080/namer-stats"
for i in range(0, 1000):
for i in range(0, 10):
data = get_data(i, resource_uri)
post_to_statwriter(data, statwriter_uri)
time.sleep(2)
Expand Down

0 comments on commit a25f6c5

Please sign in to comment.