예제 #1
0
 def testSimpleArgAndKwArg(self):
     x = 9
     y = 'hey'
     rc = RetryingCall(lambda x, y=None: (x, y), x, y=y)
     d = rc.start()
     d.addCallback(lambda r: self.assertEqual(r, (x, y)))
     return d
예제 #2
0
 def testDontAllowKeyError(self):
     def _failureTester(f):
         f.trap(ValueError)
     f = InitiallyFailing(3, exceptionList=[KeyError])
     rc = RetryingCall(f)
     d = rc.start(failureTester=_failureTester)
     self.failUnlessFailure(d, KeyError)
     return d
예제 #3
0
파일: twitter.py 프로젝트: jdunck/Tickery
def userByScreenname(screenname):
    r = RetryingCall(
        client.getPage, "%s/users/show.json?screen_name=%s" % (TWITTER_URL, urllib.quote(screenname.encode("utf-8")))
    )
    tester = AllowOne404Tester()
    d = r.start(failureTester=tester.test)
    d.addCallback(lambda j: json.loads(j))
    return d
예제 #4
0
 def testExactlyOneSuccessfulCall(self):
     result = 99
     f = CallCounter()
     rc = RetryingCall(f, result)
     d = rc.start()
     d.addCallback(lambda r: self.assertEqual(r, result))
     d.addCallback(lambda _: f.assertCalledOnce())
     return d
예제 #5
0
파일: twitter.py 프로젝트: jkakar/Tickery
def userById(uid):
    r = RetryingCall(
        client.getPage,
        '%s/users/show.json?user_id=%s' % (TWITTER_API_URL, uid))
    tester = AllowOne404Tester()
    d = r.start(failureTester=tester.test)
    d.addCallback(lambda j: json.loads(j))
    return d
예제 #6
0
 def test1ValueError(self):
     def _failureTester(f):
         f.trap(ValueError)
     result = 5
     f = InitiallyFailing(1, result=result, exceptionList=[ValueError])
     rc = RetryingCall(f)
     d = rc.start(failureTester=_failureTester)
     d.addCallback(lambda r: self.assertEqual(r, result))
     d.addCallback(lambda _: f.assertMaximallyFailed())
     return d
예제 #7
0
 def testIgnoreRegularException(self):
     def _failureTester(f):
         f.trap(Exception)
     result = 5
     f = InitiallyFailing(3, result=result)
     rc = RetryingCall(f)
     d = rc.start(failureTester=_failureTester)
     d.addCallback(lambda r: self.assertEqual(r, result))
     d.addCallback(lambda _: f.assertMaximallyFailed())
     return d
예제 #8
0
 def testSimpleDeferredReturner(self):
     result1 = 15
     result2 = 16
     def _ret(result):
         self.assertEqual(result, result1)
         return result2
     rc = RetryingCall(lambda: defer.succeed(result1))
     d = rc.start()
     d.addCallback(_ret)
     d.addCallback(lambda r: self.assertEqual(r, result2))
     return d
예제 #9
0
 def testValueErrorThenNameError(self):
     def _failureTester(f):
         if not f.check(ValueError, NameError):
             return f
     result = 15
     f = InitiallyFailing(2, result=result,
                          exceptionList=[ValueError, NameError])
     rc = RetryingCall(f)
     d = rc.start(failureTester=_failureTester)
     d.addCallback(lambda r: self.assertEqual(r, result))
     d.addCallback(lambda _: f.assertMaximallyFailed())
     return d
예제 #10
0
파일: callback.py 프로젝트: jkakar/Tickery
 def _storeAccessToken(self, result, request):
     accessToken = oauth.OAuthToken.from_string(result)
     log.msg('Got access token: %s' % accessToken)
     oaRequest = oauth.OAuthRequest.from_consumer_and_token(
         consumer.consumer, token=accessToken,
         http_url=twitter.VERIFY_CREDENTIALS_URL)
     oaRequest.sign_request(
         signature.hmac_sha1, consumer.consumer, accessToken)
     log.msg('Verifying credentials.')
     r = RetryingCall(client.getPage, oaRequest.to_url())
     d = r.start()
     d.addCallback(self._storeUser, accessToken, request)
     d.addErrback(log.err)
     return d
예제 #11
0
파일: callback.py 프로젝트: jkakar/Tickery
    def render_GET(self, request):
        # The cookie cache is actually only available once the cache
        # service has started. But that should happen before we get any
        # requests.
        log.err('Callback received: %s' % request)

        oauthToken = request.args['oauth_token']
        if oauthToken:
            oauthToken = oauthToken[0]
        else:
            log.err('Received callback with no oauth_token: %s' % request)
            raise Exception('Received callback with no oauth_token.')

        oauthVerifier = request.args['oauth_verifier']
        if oauthVerifier:
            oauthVerifier = oauthVerifier[0]
        else:
            log.err('Received callback with no oauth_verifier: %s' % request)
            raise Exception('Received callback with no oauth_verifier.')

        try:
            token = self.cache.oauthTokenDict.pop(oauthToken)
        except KeyError:
            log.err('Received callback with unknown oauth_token: %s' %
                    oauthToken)
            raise Exception('Received callback with unknown oauth_token.')

        oaRequest = oauth.OAuthRequest.from_consumer_and_token(
            consumer.consumer, token=token, verifier=oauthVerifier,
            http_url=twitter.ACCESS_TOKEN_URL)
        oaRequest.sign_request(
            signature.hmac_sha1, consumer.consumer, token)
        log.msg('Requesting access token.')
        r = RetryingCall(
            client.getPage, oaRequest.to_url(), headers=oaRequest.to_header())
        d = r.start()
        d.addCallback(self._storeAccessToken, request)
        d.addErrback(log.err)
        return server.NOT_DONE_YET
예제 #12
0
파일: login.py 프로젝트: jdunck/Tickery
def getTwitterOAuthURL(oauthTokenDict):
    log.msg('Got login URL request.')

    def _cb(result):
        token = oauth.OAuthToken.from_string(result)
        # Store the token so we have the oauth_secret when the callback comes.
        oauthTokenDict[token.key] = token
        request = oauth.OAuthRequest.from_token_and_callback(
            token=token, http_url=twitter.AUTHORIZATION_URL)
        url = request.to_url()
        log.msg('Browser Twitter auth redirect URL = %r' % url)
        return url

    request = oauth.OAuthRequest.from_consumer_and_token(
        consumer.consumer, callback=defaults.TICKERY_CALLBACK_URL,
        http_url=twitter.REQUEST_TOKEN_URL)
    request.sign_request(signature.hmac_sha1, consumer.consumer, None)

    r = RetryingCall(
        client.getPage, twitter.REQUEST_TOKEN_URL, headers=request.to_header())
    d = r.start()
    d.addCallback(_cb)
    return d
예제 #13
0
 def testSimplestDeferredReturner(self):
     result = 15
     rc = RetryingCall(lambda: defer.succeed(result))
     d = rc.start()
     d.addCallback(lambda r: self.assertEqual(r, result))
     return d
예제 #14
0
 def testSimplestNoFailure(self):
     result = 15
     rc = RetryingCall(lambda: result)
     d = rc.start()
     d.addCallback(lambda r: self.assertEqual(r, result))
     return d
예제 #15
0
 def testListBackoffIteratorAsList(self):
     rc = RetryingCall(lambda: defer.fail(Exception()))
     d = rc.start(backoffIterator=[0.05, 0.06, 0.07])
     self.failUnlessFailure(d, Exception)
     return d
예제 #16
0
 def testListBackoffIteratorAsTuple(self):
     rc = RetryingCall(lambda: defer.fail(Exception()))
     d = rc.start(backoffIterator=(0.01, 0.01, 0.01))
     self.failUnlessFailure(d, Exception)
     return d
예제 #17
0
 def testInitiallyFailing0Failures(self):
     f = InitiallyFailing(0)
     rc = RetryingCall(f)
     d = rc.start()
     d.addCallback(lambda _: f.assertMaximallyFailed())
     return d
예제 #18
0
 def testFirstFailureReceived(self):
     f = ValueErrorThenNameErrorRaiser()
     rc = RetryingCall(f)
     d = rc.start(backoffIterator=(0.01, 0.01, 0.01))
     self.failUnlessFailure(d, ValueError)
     return d
예제 #19
0
 def testSimpleKwArgs(self):
     result = 9
     rc = RetryingCall(lambda xxx=None: xxx, xxx=result)
     d = rc.start()
     d.addCallback(lambda r: self.assertEqual(r, result))
     return d