import requests
import json


def create_snippet(data, baseurl, timeout, raw):
    """
    Creates snippet with the given data on the haste server specified by the
    baseurl and returns URL of the created snippet.
    """
    try:
        url = baseurl + "/documents"
        response = requests.post(url, data.encode('utf-8'), timeout=float(timeout))
    except requests.exceptions.Timeout:
        exit("Error: connection timed out")

    dockey = json.loads(response.text)['key']
    return baseurl + ("/raw/" if raw else "/") + dockey