示例#1
0
def login(user_id, on_token, on_error=None):
    errback = on_error or make_errback()
    try:
        def handle_response(response):
            if response.code == 200:
                callback = partial(
                    _handle_login,
                    on_token=on_token,
                    on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                print('error %d %s -- login' % (response.code, response.phrase))
                errback('returned %s' % response.code, code=response.code)

        agent = Agent(reactor)
        d = agent.request(
            'POST',
            endpoint('login'),
            bodyProducer=JsonProducer({'key': user_id}))
        d.addCallbacks(handle_response, errback)
        timeoutCall = reactor.callLater(10, d.cancel)
        def completed(passthrough):
            print 'Login connection timeout'
            if timeoutCall.active():
                timeoutCall.cancel()

            return passthrough
        d.addBoth(completed)
    except Exception as ex:
        print('error %s -- login' % ex)
        if on_error:
            errback('error %s -- login' % ex, 'login')
        else:
            errback('error %s' % ex, 'login')
示例#2
0
def register(pub_key, on_uid, on_error=None):
    errback = on_error or make_errback()
    try:

        def handle_response(response):
            if response.code == 200:
                callback = partial(
                    _handle_register,
                    on_uid=on_uid,
                    on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                print('error %d %s -- register' % (response.code, response.phrase))
                errback('returned %s' % response.code, code=response.code)

        agent = Agent(reactor)
        d = agent.request(
            'POST',
            endpoint('register'),
            bodyProducer=JsonProducer({'pub_key': pub_key}))
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        print('error %s -- register' % ex)
        if on_error:
            errback('error %s -- register' % ex)
        else:
            errback('error %s' % ex, 'register')
示例#3
0
def post(url, data=None, on_created=None, on_error=None):
    errback = on_error or make_errback(frames_back=2)
    try:
        def handle_response(response):
            if response.code == 200:
                callback = partial(
                    _handle_post,
                    on_created=on_created,
                    on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                errback('returned %s' % response.code)

        agent = Agent(reactor)
        headers = Headers(get_auth_headers())
        headers.addRawHeader('Content-Type', 'application/json')
        if not data:
            data = {'dummy': 1}
        d = agent.request(
            'POST',
            add_params(url, rid=uuid4().hex),
            headers=headers,
            bodyProducer=JsonProducer(data) if data else None)
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        errback('error %s' % ex)
示例#4
0
def post(url, data=None, on_created=None, on_error=None):
    errback = on_error or make_errback(frames_back=2)
    try:

        def handle_response(response):
            if response.code == 200:
                callback = partial(_handle_post,
                                   on_created=on_created,
                                   on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                errback('returned %s' % response.code)

        agent = Agent(reactor)
        headers = Headers(get_auth_headers())
        headers.addRawHeader('Content-Type', 'application/json')
        if not data:
            data = {'dummy': 1}
        d = agent.request('POST',
                          add_params(url, rid=uuid4().hex),
                          headers=headers,
                          bodyProducer=JsonProducer(data) if data else None)
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        errback('error %s' % ex)
示例#5
0
def post(text):
    try:
        def handle_response(response):
            if not hasattr(response, 'code'):
                Logger.warning('google_analytics.post(): got %s' % (str(response)))
                return
            if response.code / 100 != 2:
                Logger.warning('google_analytics.post(): got %s: %s' % (response.code, response))

        from config import app_version, app_platform
        text_lower = text.lower()
        if 'screen' in text_lower and 'exit' not in text_lower:
            body = urlencode({
                'v': '1',
                'tid': tracking_id,
                'cid': client_id,
                'ds': 'app',
                't': 'screenview',
                'an': 'Insiderr',
                'aid': 'com.insiderr.app.%s' % app_platform,
                'av': str(app_version),
                'cd': text,
            })
        else:
            body = urlencode({
                'v': '1',
                'tid': tracking_id,
                'cid': client_id,
                'ds': 'app',
                't': 'event',
                'an': 'Insiderr',
                'aid': 'com.insiderr.app.%s' % app_platform,
                'av': str(app_version),
                'ec': 'debug-%s' % app_platform, #category
                'ea': text,
                #'el': 'label',
            })

        agent = Agent(reactor)
        headers = Headers()
        #headers.addRawHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36')
        d = agent.request(
            method='POST',
            uri=url,
            headers=headers,
            bodyProducer=StringProducer(body))
        d.addBoth(handle_response)
    except Exception as ex:
        Logger.error('google_analytics.post(): exception %s' % ex)
示例#6
0
def delete(url, on_created=None, on_error=None):
    errback = on_error or make_errback(frames_back=2)
    try:
        def handle_response(response):
            if response.code == 200:
                callback = partial(
                    _handle_delete,
                    on_created=on_created,
                    on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                errback('returned %s' % response.code)
        agent = Agent(reactor)
        headers = Headers(get_auth_headers())
        d = agent.request(
            'DELETE',
            add_params(url, rid=uuid4().hex),
            headers=headers)
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        errback('error %s' % ex)
示例#7
0
def get(url, data=None, on_response=None, on_error=None):
    errback = on_error or make_errback(frames_back=2)
    try:

        def handle_response(response):
            if response.code == 200:
                response.deliverBody(JsonReceiver.create(on_response, errback))
            else:
                errback('returned %s' % response.code)

        agent = ContentDecoderAgent(Agent(reactor, pool=pool),
                                    [('gzip', GzipDecoder)])
        headers = Headers(get_auth_headers())
        headers.addRawHeader('User-Agent', 'gzip')
        d = agent.request('GET',
                          url,
                          headers=headers,
                          bodyProducer=JsonProducer(data) if data else None)
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        errback('error %s' % ex)