2
votes

I have a Python script that I would like to run at a set interval using Google Cloud Functions and Google Cloud Scheduler. The script works fine when tested locally, but when I test it in the Google Cloud Functions panel I'm getting a network connection error message for some reason? Do I need to do something special to get the requests library to work when the Python script is a Google Cloud Function?

Python script:

import datetime
from config import config_vars
import requests

APIKEY = config_vars['APIKEY']
NOW = datetime.datetime.now()
LAST = NOW - datetime.timedelta(seconds=config_vars['UPDATE_INTERVAL'])

def getOrders(nextPage = None):
  url = "https://api.squarespace.com/1.0/commerce/orders"
  if nextPage is None:
    params = {
      'modifiedAfter': f"{LAST.isoformat()}Z",
      'modifiedBefore': f"{NOW.isoformat()}Z"
    }
  else:
    params = { 'cursor': nextPage }
  headers = { "Authorization": f"Bearer {SAPIKEY}" }
  r = requests.get(url, params=params, headers=headers)
  if not r.ok:
    logging.error(f"Unable to get orders. Respoonse: {r.text}")
    return []
  res = r.json()
  pagination = res['pagination']
  if pagination['hasNextPage']: return res['result'] + getOrders(pagination['nextPageCursor'])
  else: return res['result']

def main(data = None, context = None):
  """Triggered from a message on a Cloud Pub/Sub topic.
  Args:
    data (dict): Event payload.
    context (google.cloud.functions.Context): Metadata for the event.
  """
  orders = getOrders()
  for order in orders:
    # do something with each order
    pass

if __name__ == '__main__': main()

Error message:

HTTPSConnectionPool(host='api.squarespace.com', port=443): Max retries exceeded with url: /1.0/commerce/orders?modifiedAfter=2020-02-09T23%3A01%3A44.372818Z&modifiedBefore=2020-02-09T23%3A01%3A45.372818Z (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eedecb76850>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

1
Do you have billing enabled for your project? You won't be able to make outbound requests until you do. - Dustin Ingram
Also, does running it on Google Cloud Scheduler cause a change to the registered user agent? If so, try adding your own user agent data to the custom headers requests.readthedocs.io/en/master/user/quickstart/… - Matt L.
What else is in your Cloud Functions logs that traces the entire invocation from start to finish? - Doug Stevenson
@DustinIngram Thanks, That was it and now I feel dumb. I knew I couldn't use the cloud scheduler to call it until enabling billing, but for some reason I thought I'd be able to test the function first - Nick Gazzillo

1 Answers

1
votes

You need to enable billing for your project. You won't be able to make outbound requests to any URL until it is enabled.