import json
import requests


def start_stream(project, location, token, s_config):
    """
    This function will start the stream in Google Cloud DataStream
    :param project: Google Cloud project id mentioned in variables.py
    :param location: Google Cloud resource location, for example us-central1
    :param token: Google Cloud auth token
    :param s_config: stream config from variables.py
    :return: True or False
    """

    stream_id = s_config["stream_id"]
    name = s_config["stream_name"]

    url = f"https://datastream.googleapis.com/v1/" \
          f"projects/{project}/locations/{location}/streams/{stream_id}?" \
          "updateMask=state"

    payload = json.dumps({
        "state": "RUNNING"
    })
    headers = {
        'Authorization': token,
        'Content-Type': 'application/json'
    }

    response = requests.request("PATCH", url, headers=headers, data=payload)

    if response.status_code == 200:
        print(f"Stream {name} started successfully")
        start_stream_stat = True
    else:
        print(f"Issue while starting stream: {response.text}")
        start_stream_stat = False
    return start_stream_stat