can someone point me to a python script that calls...
# integrations
c
can someone point me to a python script that calls the rest api and works with TBA and pagination ? I got the whole thing working only pagination is a real pain as the authentication keeps on failing. Works in postman but not in my script… 😵‍💫
got it working 🥳
Copy code
import requests
import json
import pandas as pd
from pymongo import MongoClient, UpdateOne
from oauthlib import oauth1
from requests_oauthlib import OAuth1Session

# MongoDB Atlas credentials
...

def parse_suiteql_response(response):
    response_json = json.loads(response.text)

    items = response_json['items']
    offset = response_json['offset']
    count = response_json['count']
    total = response_json['totalResults']

    if response_json["hasMore"]:
        next_link = next(link for link in response_json["links"] if link["rel"] == "next")["href"]
    else:
        next_link = None

    return items, offset, count, total, next_link

def run_suiteql_query(query):
    client = OAuth1Session(
        client_secret="..",
        client_key="..",
        resource_owner_key="..",
        resource_owner_secret="..",
        realm="..",
        signature_method=oauth1.SIGNATURE_HMAC_SHA256
    )

    url_account = ".."
    url = f"https://{url_account}.<http://suitetalk.api.netsuite.com/services/rest/query/v1/suiteql|suitetalk.api.netsuite.com/services/rest/query/v1/suiteql>"

    headers = {
        "Prefer": "transient",
        "Content-Type": "application/json"
    }

    body = json.dumps({"q": query})
    data = []

    while True:
        response = <http://client.post|client.post>(url=url, data=body, headers=headers)

        try: 
            response.raise_for_status()
        except requests.exceptions.HTTPError as e: 
            raise Exception(f"SuiteQL request failed. {e}. Response Body: {response.text}")

        items, offset, count, total, next_link = parse_suiteql_response(response)

        print(f"Retrieved {offset + count} of {total} results")

        data = data + items

        if next_link:
            url = next_link
        else:
            break

    return data

.....
👀 1
m
Try this lib it worked for me really good and has a module to cal suiteql https://pypi.org/project/NetSuite-Connector/