2

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'))

4
  • 7
    Do you have billing enabled for your project? You won't be able to make outbound requests until you do. Commented Feb 9, 2020 at 23:31
  • 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/… Commented Feb 9, 2020 at 23:33
  • What else is in your Cloud Functions logs that traces the entire invocation from start to finish? Commented Feb 10, 2020 at 0:01
  • @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 Commented Feb 10, 2020 at 0:05

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

The "Billing enabled" answer worked for me initially - However, I was mystified by a later occurrence of this same message on a Function / project where billing was definitely enabled, and in fact I could make some outbound requests, but one in particular was failing. It turned out to be a \n at the end of the URL string I had been sending to the function as a parameter. In my particular case, since I was using PHP to generate the string, a simple trim() call removed the cruft and the function continued to work as expected. Posting just in case it helps someone else, as this had me scratching my head for a bit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.