import requests
import xmlrpc.client

ODOO_URL = 'http://localhost:16001'
ODOO_DB = 'hazard_new'
ODOO_USER = 'admin'
ODOO_PASS = 'admin'

def get_shopify_credentials():
    common = xmlrpc.client.ServerProxy(f'{ODOO_URL}/xmlrpc/2/common')
    uid = common.authenticate(ODOO_DB, ODOO_USER, ODOO_PASS, {})
    models = xmlrpc.client.ServerProxy(f'{ODOO_URL}/xmlrpc/2/object')

    config_ids = models.execute_kw(ODOO_DB, uid, ODOO_PASS, 'shopify.config', 'search', [[]])
    if not config_ids:
        return None, None
    config = models.execute_kw(ODOO_DB, uid, ODOO_PASS, 'shopify.config', 'read', [config_ids[0]], {'fields': ['shop_url', 'access_token']})[0]
    return config.get('shop_url'), config.get('access_token')

def get_collections(shop_url, access_token):
    # Using GraphQL
    query = """
    {
      collections(first: 200, query: "title:*78*") {
        edges {
          node {
            id
            title
          }
        }
      }
    }
    """
    url = f"https://{shop_url}/admin/api/2023-10/graphql.json"
    headers = {
        'X-Shopify-Access-Token': access_token,
        'Content-Type': 'application/json',
    }
    resp = requests.post(url, headers=headers, json={'query': query})
    for edge in resp.json().get('data', {}).get('collections', {}).get('edges', []):
        print(f"ID: {edge['node']['id']}, Title: {edge['node']['title']}")

if __name__ == '__main__':
    shop_url, access_token = get_shopify_credentials()
    if shop_url:
        get_collections(shop_url, access_token)
