import json
import requests


DWEET_UID = "5410ab1e-319c-4f14-a2e4-04725df69121"

class DweetServer(object):
    "Gets state from server and sends it to the server after change"
    def __init__(self, controller_id=None):
        self.url_base = "https://dweet.io:443/dweet/quietly/for/%s" % DWEET_UID
        self.controller_id = controller_id

    def send_message(self, message):
        result = requests.post(self.full_url(),
        json=json.dumps({
          'controller_id': self.controller_id,
          'message': message
        }))
        return handle_response(result)

    def full_url(self):
        if self.controller_id is None:
            raise ClassNotReadyException("Controller id not set!")
        if self.url_base is None:
            raise ClassNotReadyException("URL base not set!")
        return self.url_base


def handle_response(response):
    if response.status_code != 200 or response.status_code != 204:
        raise Exception("Response not 200 or 204!")
    else:
        return response.json()
