import requests
import json


def _get_sample_service_url(sw_url, ss_release):
    """Fetch the latest URL for the sample service using the ServiceWizard"""
    payload = {
        "method": "ServiceWizard.get_service_status",
        "id": '',
        "params": [{"module_name": "SampleService", "version": ss_release}],
        "version": "1.1"
    }
    headers = {'Content-Type': 'application/json'}
    sw_resp = requests.post(url=sw_url, headers=headers, data=json.dumps(payload))
    if not sw_resp.ok:
        raise RuntimeError(f"ServiceWizard error, with code {sw_resp.status_code}. \n{sw_resp.text}")
    wiz_resp = sw_resp.json()
    if wiz_resp.get('error'):
        raise RuntimeError(f"ServiceWizard {sw_url} with params"
                           f" {json.dumps(payload)} Error - " + str(wiz_resp['error']))
    return wiz_resp['result'][0]['url']