Ejemplo n.º 1
0
    def __init__(self, config):
        self.api_key = config["apiKey"]
        self.auth_domain = config["authDomain"]
        self.database_url = config["databaseURL"]
        self.storage_bucket = config["storageBucket"]
        self.credentials = None
        self.requests = requests.Session()
        if config.get("serviceAccount"):
            scopes = [
                'https://www.googleapis.com/auth/firebase.database',
                'https://www.googleapis.com/auth/userinfo.email',
                "https://www.googleapis.com/auth/cloud-platform"
            ]
            service_account_type = type(config["serviceAccount"])
            if service_account_type is str:
                self.credentials = ServiceAccountCredentials.from_json_keyfile_name(config["serviceAccount"], scopes)
            if service_account_type is dict:
                self.credentials = ServiceAccountCredentials.from_json_keyfile_dict(config["serviceAccount"], scopes)
        if is_appengine_sandbox():
            # Fix error in standard GAE environment
            # is releated to https://github.com/kennethreitz/requests/issues/3187
            # ProtocolError('Connection aborted.', error(13, 'Permission denied'))
            adapter = appengine.AppEngineAdapter(max_retries=3)
        else:
            adapter = requests.adapters.HTTPAdapter(max_retries=3)

        for scheme in ('http://', 'https://'):
            self.requests.mount(scheme, adapter)
Ejemplo n.º 2
0
    def __init__(self, config):
        self.api_key = config["apiKey"]
        self.auth_domain = config["authDomain"]
        self.database_url = config["databaseURL"]
        self.storage_bucket = config["storageBucket"]
        self.credentials = None
        self.requests = requests.Session()

        if config.get("serviceAccount"):
            scopes = [
                'https://www.googleapis.com/auth/firebase.database',
                'https://www.googleapis.com/auth/userinfo.email',
                "https://www.googleapis.com/auth/cloud-platform"
            ]

            service_account_type = type(config["serviceAccount"])

            if service_account_type is str:
                self.credentials = ServiceAccountCredentials.from_json_keyfile_name(config["serviceAccount"], scopes)

            if service_account_type is dict:
                self.credentials = ServiceAccountCredentials.from_json_keyfile_dict(config["serviceAccount"], scopes)

        if is_appengine_sandbox():
            adapter = appengine.AppEngineAdapter(max_retries=3)
        else:
            adapter = requests.adapters.HTTPAdapter(max_retries=3)

        for scheme in ('http://', 'https://'):
            self.requests.mount(scheme, adapter)
Ejemplo n.º 3
0
def test_get(mock_urlfetch):
    """Tests a simple requests.get() call.

    App Engine urlfetch docs:
    https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.urlfetch
    """
    response = mock.Mock(status_code=200, content='asdf', headers={})
    mock_urlfetch.fetch = mock.Mock(return_value=response)

    session = requests.Session()
    session.mount('http://', appengine.AppEngineAdapter())
    resp = session.get('http://url/', timeout=9, headers={'Foo': 'bar'})
    assert resp.status_code == 200
    assert resp.content == 'asdf'

    args, kwargs = mock_urlfetch.fetch.call_args
    assert args == ('http://url/', )
    assert kwargs['deadline'] == 9
    assert kwargs['headers']['Foo'] == 'bar'
Ejemplo n.º 4
0
import logging
import os

from flask import Flask, render_template
from flask_restful import Api
from google.appengine.api import app_identity

from hello.messages import HelloWorld, HelloMessage, db

import requests
from requests_toolbelt.adapters import appengine
from urllib import quote_plus as urlquote

s = requests.Session()
s.mount('http://', appengine.AppEngineAdapter())
s.mount('https://', appengine.AppEngineAdapter())
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
appengine.monkeypatch()

app = Flask(__name__)

# local setting
url_prefix = "http://"
app.config[
    'SQLALCHEMY_DATABASE_URI'] = 'mysql://*****:*****@localhost/gae-starter'
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
    # app engine setting
    url_prefix = "https://"
    app.config[
        'SQLALCHEMY_DATABASE_URI'] = 'mysql://*****:*****@/gae-starter?unix_socket=/cloudsql/legaldocker:us-central1:legaldocker'