1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import google
.appengine
.api
.urlfetch
as urlfetch
10 from future
import Future
11 from url_fetcher
import UrlFetcher
15 _RETRY_DELAY_SECONDS
= 30
18 class UrlFetcherAppengine(UrlFetcher
):
19 """An implementation of UrlFetcher which uses the GAE urlfetch API to
23 self
._retries
_left
= _MAX_RETRIES
25 def FetchImpl(self
, url
, headers
):
26 return urlfetch
.fetch(url
, deadline
=20, headers
=headers
)
28 def FetchAsyncImpl(self
, url
, headers
):
29 def process_result(result
):
30 if result
.status_code
== 429:
31 if self
._retries
_left
== 0:
32 logging
.error('Still throttled. Giving up.')
34 self
._retries
_left
-= 1
35 logging
.info('Throttled. Trying again in %s seconds.' %
37 time
.sleep(_RETRY_DELAY_SECONDS
)
38 return self
.FetchAsync(url
, username
, password
, access_token
).Get()
41 rpc
= urlfetch
.create_rpc(deadline
=20)
42 urlfetch
.make_fetch_call(rpc
, url
, headers
=headers
)
43 return Future(callback
=lambda: process_result(rpc
.get_result()))