示例#1
0
 def test_timeout_once(self):
     response = http.client.HTTPResponse(mock.MagicMock())
     response.status = 200
     with mock.patch('urllib.request.urlopen', side_effect=[None,
                                                            response]):
         self.assertIs(net_utils.retrying_urlopen('http://nowhere.com'),
                       response)
示例#2
0
def _fetch_candles(params):
    """Fetch the given URL from OANDA and return a list of (utc-time, price).

    Args:
      params: A dict of URL params values.
    Returns:
      A sorted list of (time, price) points.
    """

    url = '?'.join((URL, parse.urlencode(sorted(params.items()))))
    logging.info("Fetching '%s'", url)

    # Fetch the data.
    response = net_utils.retrying_urlopen(url)
    if response is None:
        return None
    data_string = response.read().decode('utf-8')

    # Parse it.
    data = json.loads(data_string, parse_float=Decimal)
    try:
        # Find the candle with the latest time before the given time we're searching
        # for.
        time_prices = []
        candles = sorted(data['candles'], key=lambda candle: candle['time'])
        for candle in candles:
            candle_dt_utc = datetime.datetime.strptime(
                candle['time'], r"%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=tz.tzutc())
            candle_price = Decimal(candle['openMid'])
            time_prices.append((candle_dt_utc, candle_price))
    except KeyError:
        logging.error("Unexpected response data: %s", data)
        return None
    return sorted(time_prices)
示例#3
0
 def test_max_retry(self):
     with mock.patch('urllib.request.urlopen',
                     side_effect=[None, None, None, None, None, None]):
         self.assertIsNone(net_utils.retrying_urlopen('http://nowhere.com'))
示例#4
0
 def test_success_other(self):
     response = http.client.HTTPResponse(mock.MagicMock())
     with mock.patch('urllib.request.urlopen', return_value=response):
         self.assertIsNone(net_utils.retrying_urlopen('http://nowhere.com'))