# Copyright 2012 Kelcey Damage & Kraig Amador
# Source: http://goo.gl/KQUeMd

import base64
import hmac
import hashlib
import json
import urllib


class CloudStack(object):

    def __init__(self, api_url, api_key, secret):
        self.api_url = api_url
        self.api_key = api_key
        self.secret = secret

    def encode_user_data(self, data):
        return base64.b64encode(data)

    def request(self, args):
        args["apiKey"] = self.api_key
        self.params = []
        self._sort_request(args)
        self._create_signature()
        self._build_post_request()

    def _sort_request(self, args):
        keys = sorted(args.keys())
        for key in keys:
            self.params.append(key + "=" + urllib.quote_plus(args[key]))

    def _create_signature(self):
        self.query = "&".join(self.params)
        digest = hmac.new(self.secret, msg=self.query.lower(),
                          digestmod=hashlib.sha1).digest()
        self.signature = base64.b64encode(digest)

    def _build_post_request(self):
        self.query += "&signature=" + urllib.quote_plus(self.signature)
        self.value = self.api_url + "?" + self.query

    def __getattr__(self, name):
        def handler(*args, **kwargs):
            if kwargs:
                return self._make_request(name, kwargs)
            return self._make_request(name, args[0])
        return handler

    def _http_get(self, url):
        response = urllib.urlopen(url)
        return response.read()

    def _make_request(self, command, args):
        args["response"] = "json"
        args["command"] = command
        self.request(args)
        data = self._http_get(self.value)
        key = command.lower() + "response"
        return json.loads(data)[key]
