Beispiel #1
0
    def __init__(self, api_key=None, proxy_dict=None, env=None, json_encoder=None, adapter=None):
        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv('FCM_API_KEY', None):
            self._FCM_API_KEY = os.getenv('FCM_API_KEY', None)
        else:
            raise AuthenticationError("Please provide the api_key in the google-services.json file")

        self.FCM_REQ_PROXIES = None
        self.requests_session = requests.Session()
        retries = Retry(backoff_factor=1, status_forcelist=[502, 503],
                        allowed_methods=(Retry.DEFAULT_ALLOWED_METHODS | frozenset(['POST'])))
        self.requests_session.mount('http://', adapter or HTTPAdapter(max_retries=retries))
        self.requests_session.mount('https://', adapter or HTTPAdapter(max_retries=retries))
        self.requests_session.headers.update(self.request_headers())
        self.requests_session.mount(self.INFO_END_POINT, HTTPAdapter(max_retries=self.INFO_RETRIES))

        if proxy_dict and isinstance(proxy_dict, dict) and (('http' in proxy_dict) or ('https' in proxy_dict)):
            self.FCM_REQ_PROXIES = proxy_dict
            self.requests_session.proxies.update(proxy_dict)
        self.send_request_responses = []

        if env == 'app_engine':
            try:
                from requests_toolbelt.adapters import appengine
                appengine.monkeypatch()
            except ModuleNotFoundError:
                pass

        self.json_encoder = json_encoder
Beispiel #2
0
    def __init__(self,
                 api_key=None,
                 proxy_dict=None,
                 env=None,
                 json_encoder=None):
        """

        :type proxy_dict: dict, api_key: string
        """
        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv('FCM_API_KEY', None):
            self._FCM_API_KEY = os.getenv('FCM_API_KEY', None)
        else:
            raise AuthenticationError(
                "Please provide the api_key in the google-services.json file")
        self.FCM_REQ_PROXIES = None
        if proxy_dict and isinstance(proxy_dict, dict) and (
            ('http' in proxy_dict) or ('https' in proxy_dict)):
            self.FCM_REQ_PROXIES = proxy_dict
        self.send_request_responses = list()
        if env == 'app_engine':
            try:
                from requests_toolbelt.adapters import appengine
                appengine.monkeypatch()
            except:
                pass
        self.json_encoder = json_encoder
Beispiel #3
0
    def __init__(self,
                 api_key=None,
                 proxy_dict=None,
                 env=None,
                 json_encoder=None):
        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv('FCM_API_KEY', None):
            self._FCM_API_KEY = os.getenv('FCM_API_KEY', None)
        else:
            raise AuthenticationError(
                "Please provide the api_key in the google-services.json file")

        self.FCM_REQ_PROXIES = None
        self.requests_session = requests.Session()
        self.requests_session.headers.update(self.request_headers())
        self.requests_session.mount(self.INFO_END_POINT,
                                    HTTPAdapter(max_retries=self.INFO_RETRIES))

        if proxy_dict and isinstance(proxy_dict, dict) and (
            ('http' in proxy_dict) or ('https' in proxy_dict)):
            self.FCM_REQ_PROXIES = proxy_dict
            self.requests_session.proxies.update(proxy_dict)
        self.send_request_responses = list()

        if env == 'app_engine':
            try:
                from requests_toolbelt.adapters import appengine
                appengine.monkeypatch()
            except:
                pass

        self.json_encoder = json_encoder
Beispiel #4
0
    def __init__(self, api_key=None, proxy_dict=None, env=None, json_encoder=None):
        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv('FCM_API_KEY', None):
            self._FCM_API_KEY = os.getenv('FCM_API_KEY', None)
        else:
            raise AuthenticationError("Please provide the api_key in the google-services.json file")

        self.FCM_REQ_PROXIES = None
        self.requests_session = requests.Session()
        retries = Retry(backoff_factor=1, status_forcelist=[502, 503, 504],
                        method_whitelist=(Retry.DEFAULT_METHOD_WHITELIST | frozenset(['POST'])))
        self.requests_session.mount('http://', HTTPAdapter(max_retries=retries))
        self.requests_session.mount('https://', HTTPAdapter(max_retries=retries))
        self.requests_session.headers.update(self.request_headers())
        self.requests_session.mount(self.INFO_END_POINT, HTTPAdapter(max_retries=self.INFO_RETRIES))

        if proxy_dict and isinstance(proxy_dict, dict) and (('http' in proxy_dict) or ('https' in proxy_dict)):
            self.FCM_REQ_PROXIES = proxy_dict
            self.requests_session.proxies.update(proxy_dict)
        self.send_request_responses = []

        if env == 'app_engine':
            try:
                from requests_toolbelt.adapters import appengine
                appengine.monkeypatch()
            except ModuleNotFoundError:
                pass

        self.json_encoder = json_encoder
Beispiel #5
0
    def __init__(self,
                 api_key=None,
                 proxy_dict=None,
                 env=None,
                 json_encoder=None):
        """

        :type proxy_dict: dict, api_key: string
        """
        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv("FCM_API_KEY", None):
            self._FCM_API_KEY = os.getenv("FCM_API_KEY", None)
        else:
            raise AuthenticationError(
                "Please provide the api_key in the google-services.json file")
        self.FCM_REQ_PROXIES = None
        self.requests_session = requests.Session()
        self.requests_session.headers.update(self.request_headers())
        if (proxy_dict and isinstance(proxy_dict, dict)
                and (("http" in proxy_dict) or ("https" in proxy_dict))):
            self.FCM_REQ_PROXIES = proxy_dict
            self.requests_session.proxies.update(proxy_dict)
        self.send_request_responses = list()
        if env == "app_engine":
            try:
                from requests_toolbelt.adapters import appengine

                appengine.monkeypatch()
            except:
                pass
        self.json_encoder = json_encoder
Beispiel #6
0
 def get(self):
     from requests_toolbelt.adapters import appengine
     appengine.monkeypatch()
     controller = str(self.request.get('controller_id'))
     t1 = str(self.request.get('t1'))
     t2 = str(self.request.get('t2'))
     client = bigquery.Client()
     query_job = client.query(asu_constants.QUERY.format(
         controller, t1, t2))
     results = query_job.result()
     rows = list(results)
     dict_data = []
     for i in range(len(rows)):
         dict_data.append({
             'timestamp': rows[i][0],
             'controller_id': rows[i][1],
             'ph': rows[i][2],
             'ec': rows[i][3],
             'temperature': rows[i][4]
         })
     json_data = json.dumps(dict_data, cls=MyEncoder2)
     self.response.headers.add_header("Access-Control-Allow-Origin", "*")
     self.response.headers[
         'Content-Type'] = 'application/json; charset=utf-8'
     self.response.out.write(json_data)
Beispiel #7
0
 def get(self):
     from requests_toolbelt.adapters import appengine
     appengine.monkeypatch()
     controller = str(self.request.get('controller_id'))
     t1 = str(self.request.get('t1'))
     t2 = str(self.request.get('t2'))
     #controller = 'controller1'
     #t1 = '2019-04-29T18:00:04.291333'
     #t2 = '2019-04-30T18:00:04.291333'
     client = bigquery.Client()
     query_job = client.query(
         asu_constants.QUERY_MIN.format(controller, t1, t2, controller,
                                        controller, t1, t2, controller,
                                        controller, t1, t2, controller))
     results = query_job.result()
     rows = list(results)
     dict_data = []
     for i in range(len(rows)):
         dict_data.append({
             'Column': rows[i][0],
             'DATE': rows[i][1],
             'value': rows[i][2]
         })
     json_data = json.dumps(dict_data, cls=MyEncoder2)
     self.response.headers.add_header("Access-Control-Allow-Origin", "*")
     self.response.headers[
         'Content-Type'] = 'application/json; charset=utf-8'
     self.response.out.write(json_data)
Beispiel #8
0
    def get(self):

        #os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

        appengine.monkeypatch()

        self.session[str(self.request.remote_addr)] = str(
            self.request.remote_addr)

        self.session['cstate'] = 0

        alog = Behavlog()

        alog.remoaddr = self.session[str(self.request.remote_addr)]

        templist = []

        for i in range(LIST_LENGTH):
            templist.append(-1)

        alog.vector = templist

        alog.put()

        self.redirect('/main/welcome')
Beispiel #9
0
def __fetch_get_request():
    import json
    import requests
    from requests_toolbelt.adapters import appengine
    appengine.monkeypatch()

    response = requests.get('https://api.openbrewerydb.org/breweries/5494')
    return json.loads(response.text)
Beispiel #10
0
    def __init__(self,
                 loop=None,
                 api_key=None,
                 proxy_dict=None,
                 env=None,
                 json_encoder=None,
                 adapter=None):
        self.loop = loop

        if api_key:
            self._FCM_API_KEY = api_key
        elif os.getenv('FCM_API_KEY', None):
            self._FCM_API_KEY = os.getenv('FCM_API_KEY', None)
        else:
            raise AuthenticationError(
                "Please provide the api_key in the google-services.json file")

        self.FCM_REQ_PROXIES = None
        self.requests_session = requests.Session()

        self.aiodns = AsyncResolver(loop=self.loop,
                                    nameservers=["8.8.8.8", "8.8.4.4"])
        self.aiohttp_connector = aiohttp.TCPConnector(loop=self.loop,
                                                      limit=0,
                                                      ttl_dns_cache=600,
                                                      resolver=self.aiodns,
                                                      family=socket.AF_INET)
        self.aiohttp_session = ClientSession(loop=self.loop,
                                             headers=self.request_headers(),
                                             connector=self.aiohttp_connector,
                                             connector_owner=False)

        retries = Retry(backoff_factor=1,
                        status_forcelist=[502, 503, 504],
                        method_whitelist=(Retry.DEFAULT_METHOD_WHITELIST
                                          | frozenset(['POST'])))
        self.requests_session.mount(
            'http://', adapter or HTTPAdapter(max_retries=retries))
        self.requests_session.mount(
            'https://', adapter or HTTPAdapter(max_retries=retries))
        self.requests_session.headers.update(self.request_headers())
        self.requests_session.mount(self.INFO_END_POINT,
                                    HTTPAdapter(max_retries=self.INFO_RETRIES))

        if proxy_dict and isinstance(proxy_dict, dict) and (
            ('http' in proxy_dict) or ('https' in proxy_dict)):
            self.FCM_REQ_PROXIES = proxy_dict
            self.requests_session.proxies.update(proxy_dict)
        self.send_request_responses = []

        if env == 'app_engine':
            try:
                from requests_toolbelt.adapters import appengine
                appengine.monkeypatch()
            except ModuleNotFoundError:
                pass

        self.json_encoder = json_encoder
Beispiel #11
0
def setup_appengine(app):
    r"""
    App Engine setup procedures for app.
    """

    appengine.monkeypatch()
    warnings.filterwarnings('ignore', r'urllib3 is using URLFetch', urllib3.contrib.appengine.AppEnginePlatformWarning)

    app.config.setdefault('DEBUG', os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'))
    app.debug = app.config['DEBUG']
Beispiel #12
0
    def __init__(self, to_account, msg_date):
        #This line fixes the error
        #AttributeError: 'VerifiedHTTPSConnection' object has no attribute '_tunnel_host'
        appengine.monkeypatch()
        configParser = ConfigParser.RawConfigParser()
        file = r'./config/reception.cfg'
        configParser.read(file)
        self.msg_date = msg_date
        self.bucket = configParser.get('DEFAULT', 'BUCKET')
        self.dir = configParser.get('DEFAULT', 'DIRECTORY')

        for account in configParser.sections():
            matchObj = re.match('.*%s.*' % (account), to_account, re.I)
            if matchObj:
                self.bucket = configParser.get(account, 'BUCKET')
                self.dir = configParser.get(account, 'DIRECTORY')
    def __init__(self, client_id, client_secret, user, application_name):
        """Initializes an APIHandler.

    Args:
      client_id: The client id retrieved from the Cloud Console.
      client_secret: The client secret retrieved from the Cloud Console.
      user: The models.AppUser retrieved from the Datastore.
      application_name: The name of the AppEngine application.
    """
        credentials = GoogleRefreshTokenClient(client_id, client_secret,
                                               user.refresh_token)
        self.user = user
        self.client = AdManagerClient(credentials,
                                      application_name,
                                      cache=ZeepServiceProxy.NO_CACHE)
        self.page_limit = 25
        appengine.monkeypatch()
def test_appengine_monkeypatch():
    """Tests monkeypatching Requests adapters for AppEngine compatibility.
    """
    adapter = requests.sessions.HTTPAdapter

    appengine.monkeypatch()

    assert requests.sessions.HTTPAdapter == appengine.AppEngineAdapter
    assert requests.adapters.HTTPAdapter == appengine.AppEngineAdapter

    appengine.monkeypatch(validate_certificate=False)

    assert requests.sessions.HTTPAdapter == appengine.InsecureAppEngineAdapter
    assert requests.adapters.HTTPAdapter == appengine.InsecureAppEngineAdapter

    requests.sessions.HTTPAdapter = adapter
    requests.adapters.HTTPAdapter = adapter
def test_appengine_monkeypatch():
    """Tests monkeypatching Requests adapters for AppEngine compatibility.
    """
    adapter = requests.sessions.HTTPAdapter

    appengine.monkeypatch()

    assert requests.sessions.HTTPAdapter == appengine.AppEngineAdapter
    assert requests.adapters.HTTPAdapter == appengine.AppEngineAdapter

    appengine.monkeypatch(validate_certificate=False)

    assert requests.sessions.HTTPAdapter == appengine.InsecureAppEngineAdapter
    assert requests.adapters.HTTPAdapter == appengine.InsecureAppEngineAdapter

    requests.sessions.HTTPAdapter = adapter
    requests.adapters.HTTPAdapter = adapter
Beispiel #16
0
    def post(self):
        if asu_constants.SUBSCRIPTION_UNIQUE_TOKEN != self.request.get(
                'token'):
            self.response.status = 404
            return
        try:
            message = json.loads(urllib.unquote(self.request.body).rstrip('='))
            logging.info("MESSAGE")
            logging.info(message)
            controller_id = message['message']['attributes']['deviceId']
            message_body = base64.b64decode(str(message['message']['data']))
            logging.info("MESSAGE BODY")
            logging.info(message_body)
            data = json.loads(message_body)
            logging.info("DATA")
            logging.info(data)
            datetime_r = pytz.timezone('America/Phoenix').localize(
                datetime.datetime.strptime(data['timestamp'].strip(),
                                           "%Y-%m-%dT%H:%M:%S.%f"))
            nonaware_datetime_r = datetime_r.replace(
                tzinfo=None) - datetime_r.utcoffset()

            location = data['location']
            ph = data['ph']
            temperature = data['temperature']
            ec = data['ec']
            rows_to_insert = [(nonaware_datetime_r, controller_id, location,
                               ph, ec, temperature)]
        except:
            self.response.status = 200
            logging.info("ERROR")
            self.response.write('ERROR')

        from requests_toolbelt.adapters import appengine
        appengine.monkeypatch()

        client = bigquery.Client()
        dummy = client.get_table(asu_constants.TABLE_NAME)
        client.insert_rows(asu_constants.TABLE_NAME,
                           rows_to_insert,
                           selected_fields=dummy.schema)
        self.response.status = 200
        logging.info('SUCCESS')
Beispiel #17
0
def landing():
    appengine.monkeypatch()
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(BUCKET_NAME)
    blobs = bucket.list_blobs()
    photos = []
    length_folder_name = len(FOLDER_NAME)
    for blob in blobs:
        if blob.name.startswith(FOLDER_NAME):
            photos.append(blob.name)
    cached_links = []
    for photo in photos[1:]:
        blob_key = blobstore.create_gs_key("/gs/" + BUCKET_NAME + "/" +
                                           FOLDER_NAME +
                                           photo[length_folder_name:])
        cached_links.append(images.get_serving_url(blob_key, secure_url=True))
    return render_template('landing.html',
                           photos=photos,
                           cached_links=cached_links)
Beispiel #18
0
 def get(self):
     controller_id = str(self.request.get('controller_id'))
     from requests_toolbelt.adapters import appengine
     appengine.monkeypatch()
     client = bigquery.Client()
     query_job = client.query(
         asu_constants.QUERY_EIGHT_WEEKLY.format(controller_id))
     results = query_job.result()
     rows = list(results)
     dict_data = []
     for i in range(len(rows)):
         dict_data.append({
             'controller_id': rows[i][0],
             'hour_number': rows[i][1],
             'Average_ph': rows[i][2],
             'Average_ec': rows[i][3],
             'Average_temp': rows[i][4]
         })
     json_data = json.dumps(dict_data, cls=MyEncoder1)
     self.response.headers.add_header("Access-Control-Allow-Origin", "*")
     self.response.headers[
         'Content-Type'] = 'application/json; charset=utf-8'
     self.response.out.write(json_data)
Beispiel #19
0
# -*- coding: utf-8 -*-
import bot
import requests

from datastore import config
from requests_toolbelt.adapters import appengine

# Use AppEngineAdapter to support GAE with requests
appengine.monkeypatch()
app = bot.create_app(config)

if __name__ == '__main__':
    app.run()
# Load packages from virtualenv
# https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring
from google.appengine.ext import vendor
vendor.add('local')

from granary.appengine_config import *

# Make requests and urllib3 play nice with App Engine.
# https://github.com/snarfed/bridgy/issues/396
# http://stackoverflow.com/questions/34574740
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
# Disabled for now
fixed_ndb.patch_logging(0)

# Fix our runaway mapreduces
fixed_ndb.fix_rpc_ordering()

# Improve jinja2 stacktraces
fixed_jinja2.fix_stacktraces()

# Fix mapreduce to not require a certain version
fixed_mapreduce_util.patch_function()

# Make requests work with AppEngine's URLFetch
if appengine_manager.is_local_appengine():
    appengine_adapter.monkeypatch()


def webapp_add_wsgi_middleware(app):
    # Disable appstats since it may be resulting in NDB OOM issues
    # from google.appengine.ext.appstats import recording
    # app = recording.appstats_wsgi_middleware(app)

    # Clean up per-thread NDB memory "leaks", though don't try to finish all RPCs calls (which includes runaway iterators)
    app = fixed_ndb.tasklets_toplevel(app)

    # Should only use this in cases of serialized execution of requests in a multi-threaded processes.
    # So setdeploy manually, and test from there. Never a live server, as it would be both broken *and* slow.
    # from hacks import memory_leaks
    # app = memory_leaks.leak_middleware(app)
Beispiel #22
0
from django.http import HttpResponse
import json
import AuthInfoStore as AuthInfoStore
from shareplay_app.models import User
# import requests
from urlfetch import post
import spotipy
from requests_toolbelt.adapters import appengine
appengine.monkeypatch() ## needed for appengine and requests to behave

__CLIENT_ID = AuthInfoStore.CLIENT_ID
__CLIENT_SECRET = AuthInfoStore.CLIENT_SECRET
__REDIRECT_URI = AuthInfoStore.REDIRECT_URI


def pass_off(request_data):
    auth_code = request_data['auth_code']
    firebase_refresh_token = None
    if 'firebase_refresh_token' in request_data:
        firebase_refresh_token = request_data['firebase_refresh_token']

    response = post("https://accounts.spotify.com/api/token",
                    headers={'content_type': 'application/x-www-form-urlencoded'},
                    data={'grant_type': 'authorization_code',
                    'code': auth_code,
                    'redirect_uri': __REDIRECT_URI,
                    'client_id': __CLIENT_ID,
                    'client_secret': __CLIENT_SECRET})

    if response.status_code != 200:
        return HttpResponse(response.text, content_type='application/json', status=response.status_code)
Beispiel #23
0
# Disabled for now
fixed_ndb.patch_logging(0)

# Fix our runaway mapreduces
fixed_ndb.fix_rpc_ordering()

# Improve jinja2 stacktraces
fixed_jinja2.fix_stacktraces()

# Fix mapreduce to not require a certain version
fixed_mapreduce_util.patch_function()

# Make requests work with AppEngine's URLFetch
if appengine_manager.is_local_appengine():
    appengine_adapter.monkeypatch()


def webapp_add_wsgi_middleware(app):
    # Disable appstats since it may be resulting in NDB OOM issues
    # from google.appengine.ext.appstats import recording
    # app = recording.appstats_wsgi_middleware(app)

    # Clean up per-thread NDB memory "leaks", though don't try to finish all RPCs calls (which includes runaway iterators)
    app = fixed_ndb.tasklets_toplevel(app)

    # Should only use this in cases of serialized execution of requests in a multi-threaded processes.
    # So setdeploy manually, and test from there. Never a live server, as it would be both broken *and* slow.
    # from hacks import memory_leaks
    # app = memory_leaks.leak_middleware(app)
Beispiel #24
0
# from flask import Flask, request, redirect, g, render_template
import base64
# from bottle import route, run, request
import spotipy
from spotipy import oauth2
import urllib
import spotipy.util as util
from math import trunc
import unicodedata
from requests_toolbelt.adapters import appengine
import sys  #these 3 lines fix most UnicodeDecodeErrors

reload(sys)  #and UnicodeEncodeErrors due to names like
sys.setdefaultencoding("utf-8")  #Beyonce with the accent (#BeyonceError)

appengine.monkeypatch(
)  #this is a patch that allows the python requests library to be used with Google App Engine

# app = Flask(__name__)

client_id = spotify.CLIENT_ID
client_secret = spotify.CLIENT_SECRET

# comment

jinja_current_dir = jinja2.Environment(  #jinja is used for templating
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

global_dict = {}
Beispiel #25
0
# appengine_config.py
from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')
from requests_toolbelt.adapters import appengine as requests_toolbelt_appengine
requests_toolbelt_appengine.monkeypatch()
Beispiel #26
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app]
import logging

from flask import Flask
from flask import request
from flask_cors import CORS
import requests

from requests_toolbelt.adapters import appengine
appengine.monkeypatch(validate_certificate=False)
import json
import math

from google.cloud import bigquery
bigquery_client = bigquery.Client('project-shubin')

GEOCODING_API_KEY = 'AIzaSyAdaCsSyIFSoyyvIyOQEobu1twZSiQ5K8o'

app = Flask(__name__)
CORS(app)
stations = []

##Open and read file
with open('ga_stations.json', 'r') as stations_file:
    content = stations_file.read()