pythonibm-cloudibm-cloud-functions

Getting no results from an IBM Cloud Function that uses IBM Cloudant


My cloud function is meant to return all documents in the dealerships database, but I am getting {}. In the activation log its showing that all records have been sent successfully.

The problem occurs at dealers = json_result["entries"] because json_result is an empty object. Tried to Expecting value: line 1 column 1 (char 0). The serverless function is at the bottom of the file.

def get_dealerships(request):
    if request.method == "GET":
        
        url = "serverless-func-url"
        context = {
            "dealerships": get_dealers_from_cf(url),
        }
        # Prints {} evry time
        print(f'test {context["dealerships"]}') 
        return render(request, 'djangoapp/index.html', context)
def get_dealers_from_cf(url, **kwargs):
    results = []
    # Call get_request with a URL parameter
    json_result = get_request(url)
    if json_result:
        # Get the row list in JSON as dealers
        # Error
        dealers = json_result["entries"]
        # For each dealer object
        for dealer_doc in dealers:
            # Create a CarDealer object with values in `doc` object
            dealer_obj = CarDealer(
                address=dealer_doc["address"],
                city=dealer_doc["city"],
                full_name=dealer_doc["full_name"],
                id=dealer_doc["id"],
                lat=dealer_doc["lat"],
                long=dealer_doc["long"],
                short_name=dealer_doc["short_name"],
                st=dealer_doc["st"],
                zip=dealer_doc["zip"],
            )
            results.append(dealer_obj)
    return results
def get_request(url, **kwargs):
    print(kwargs)
    print("GET from {} ".format(url))
    try:
        # Call get method of requests library with URL and parameters
        response = requests.get(
            url, headers={'Content-Type': 'application/json'}, params=kwargs)
    except:
        # If any error occurs
        print("Network exception occurred")
    status_code = response.status_code
    print("With status {} ".format(status_code))
    json_data = json.loads(response.text)
    return json_data

The cloud function:

import json
from cloudant.client import Cloudant
from cloudant.error import CloudantException
from cloudant.result import Result, ResultByKey
 
def main(args):
    # Connect to the database
    client = Cloudant.iam(
        account_name='account_name',
        api_key='api_key',
        connect=True
    )
    db = client['dealerships']
 
    # Query all documents from the database
    documents = []
    for doc in db:
        documents.append(doc)
 
    # Disconnect from the database
    client.disconnect()
 
    # Return the list of documents as JSON
    return {"entries": documents}

Solution

  • You did not share enough information on how you set up the Python action in IBM Cloud Functions or how you call the action.

    Based on the error, I guess you are calling it as web action, but forgot to use the correct context extension. Does your URL end on .json?