import requests
import sys
sys.path.insert(0, r'C:\Users\Roel\clawd\scripts\demandgen-pipeline')
from config import WEBGAINS_API, PUBLISHER_ID, webgains_headers

# Try without campaign_id - publisher-level
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/feeds/products'
params = {'size': 5, 'fields[]': ['title','brand','price','id']}
r = requests.get(url, headers=webgains_headers(), params=params)
print('Publisher-level:', r.status_code)
if r.ok:
    d = r.json()
    for p in d.get('data', []):
        print(p.get('brand', ''), '|', p.get('title', ''))
else:
    print(r.text[:200])

print('---')
# Try thehike campaign (1622240)
url2 = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/1622240/feeds/products'
r2 = requests.get(url2, headers=webgains_headers(), params=params)
print('TheHike campaign:', r2.status_code)
if r2.ok:
    d2 = r2.json()
    for p in d2.get('data', []):
        print(p.get('brand', ''), '|', p.get('title', ''))
else:
    print(r2.text[:200])

print('---')
# Try POST instead of GET for manify
url3 = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/1622230/feeds/products'
body = {'size': 5, 'feeds': [18588], 'fields': ['title','brand','price','id']}
r3 = requests.post(url3, headers=webgains_headers(), json=body)
print('POST:', r3.status_code)
if r3.ok:
    d3 = r3.json()
    for p in d3.get('data', []):
        print(p.get('brand', ''), '|', p.get('title', ''))
else:
    print(r3.text[:200])

print('---')
# Try with sort parameter
params4 = {'size': 5, 'fields[]': ['title','brand','price','id'], 'sort': 'id', 'order': 'asc'}
r4 = requests.get(url3, headers=webgains_headers(), params=params4)
print('Sorted by id asc:', r4.status_code)
if r4.ok:
    d4 = r4.json()
    for p in d4.get('data', []):
        print(p.get('brand', ''), '|', p.get('title', ''), '| id:', p.get('id', ''))
