import requests
import re

# Auth
soap_auth = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:authenticate>
      <ns:customerID>35058</ns:customerID>
      <ns:passphrase>bf0b9eef0da6caf55fe614518375280fe5c4ecca</ns:passphrase>
      <ns:sandbox>false</ns:sandbox>
      <ns:locale>nl_NL</ns:locale>
      <ns:demo>false</ns:demo>
    </ns:authenticate>
  </soap:Body>
</soap:Envelope>'''

r = requests.post('https://ws.tradetracker.com/soap/affiliate',
                  data=soap_auth,
                  headers={'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'authenticate'},
                  timeout=15)
print(f'Auth: {r.status_code}')
print(f'Auth response:\n{r.text[:1000]}')
cookies = r.cookies

# Try getCampaigns with full debug
soap_campaigns = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:getCampaigns>
      <ns:campaignFilter/>
    </ns:getCampaigns>
  </soap:Body>
</soap:Envelope>'''

r2 = requests.post('https://ws.tradetracker.com/soap/affiliate',
                   data=soap_campaigns,
                   headers={'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'getCampaigns'},
                   cookies=cookies, timeout=30)
print(f'\ngetCampaigns: {r2.status_code}')
print(f'Response:\n{r2.text[:2000]}')

# Also try getAffiliateSites
soap_sites = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:getAffiliateSites>
    </ns:getAffiliateSites>
  </soap:Body>
</soap:Envelope>'''

r3 = requests.post('https://ws.tradetracker.com/soap/affiliate',
                   data=soap_sites,
                   headers={'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'getAffiliateSites'},
                   cookies=cookies, timeout=30)
print(f'\ngetAffiliateSites: {r3.status_code}')
print(f'Response:\n{r3.text[:2000]}')
