#!/usr/bin/python
# -*- coding: utf-8 -*-

__author__ = 'chenbingyu@buding.cn'

from datetime import datetime
from hashlib import md5

from requests import Request, Session


class Client(object):
    def __init__(self, app_key, app_secret, host=None):
        self.app_key = app_key
        self.app_secret = app_secret
        if host:
            self.host = host
        else:
            self.host = 'http://api.buding.cn'

    def get(self, api):
        session = Session()
        req = Request(
            'get',
            self.host + api).prepare()
        headers = self.gen_headers('GET', api, 0)
        req.headers.update(headers)
        resp = session.send(req)
        return resp.json()

    def post(self, api, **kwargs):
        session = Session()
        req = Request(
            'post',
            self.host + api,
            data=kwargs).prepare()
        headers = self.gen_headers('POST', api, req.headers['Content-Length'])
        req.headers.update(headers)
        resp = session.send(req)
        return resp.json()

    def gen_headers(self, method, api, content_length):
        headers = {}
        now = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
        signature_list = [
            method, api, now, str(content_length),
            md5.new(self.app_secret).hexdigest()]
        signature = md5.new('&'.join(signature_list)).hexdigest()

        headers['Host'] = self.host
        headers['Date'] = now
        headers['Authorization'] = '%s:%s' % (self.app_key, signature)
        return headers
@@ -500,39 +500,14 @@
 est(
-%0A            'get',%0A           
+'get',
  sel
@@ -766,40 +766,15 @@
 est(
-%0A            'post',%0A           
+'post',
  sel
@@ -786,28 +786,16 @@
 t + api,
-%0A           
  data=kw
@@ -1183,18 +1183,54 @@
 hod,
- api, now,
+%0A            api,%0A            now,%0A           
  str
@@ -1295,16 +1295,25 @@
 digest()
+%0A        
 %5D%0A      

check coding style