forked from openrightsgroup/OrgProbe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpqueue.py
37 lines (31 loc) · 1.25 KB
/
httpqueue.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
import logging
from api import RequestHttptRequest, ResponseHttptRequest
class HTTPQueue(object):
def __init__(self, probe_uuid, signer, isp):
self.probe_uuid, self.signer, self.isp = probe_uuid, signer, isp
def __iter__(self):
"""The Queue object can be used as an iterator, to fetch a test URL
from the API."""
while True:
rq = RequestHttptRequest(self.signer,
probe_uuid=self.probe_uuid,
network_name=self.isp
)
code, data = rq.execute()
if code == 404:
yield None
if code != 200:
logging.error("Error getting URLs: %s", data)
yield None
yield data
def send(self, report, urlhash=None):
"""Sends a report back to the server"""
rsp = ResponseHttptRequest(self.signer,
**report
)
rsp.execute()
def drive(self, callback):
"""This allows the queue to drive testing, by passing each
data item that is fetched from the API to a callback function."""
for data in self:
callback(data)