#!/usr/bin/env python
# -*- coding: utf-8 -*-
from services.oauth import OAuthService
import tornado.web
import tornado.gen
import tornado.escape
import json
from util.rabbitmq import send_message
from stormed import Message
import settings
from views import AsyncBaseHandler

class GitLabAuthHandler(AsyncBaseHandler):
    @tornado.gen.engine
    def _get_(self):
        return_code = self.get_argument('code')
        user_id = self.current_user.get('_id',None)
        if user_id is None:
            render_error(error_code=404,msg='not user data')
            return
        
        user = yield tornado.gen.Task(self.s_oauth.udpate_gitlab_token,user_id,{
          "code":code
        })
        
        if user is None:
            write_result(data=user)
        else:
            render_error(error_code=404,msg='not user data')
  


class GitLabTokenHandler(AsyncBaseHandler):
    s_oauth = OAuthService()
    @tornado.gen.engine
    def _get_(self):
        user_id = self.current_user.get('_id',None)
        if user_id is None:
            render_error(error_code=404,msg='not user data')
            return
        
        token = yield tornado.gen.Task(self.s_oauth.get_gitlab_token,user_id)
        
        if token is None:
            self.write_result(data=token)
        else:
            self.render_error(error_code=404,msg='not user data')

    @tornado.gen.engine
    def _post_(self):
        access_token = self.get_argument('access_token')
        token_type = self.get_argument('token_type')
        expires_in = self.get_argument('expires_in')
        refresh_token = self.get_argument('expires_in')
        user_id = self.current_user.get('_id',None)
        if user_id is None:
            render_error(error_code=404,msg='not user data')
            return
        
        user = yield tornado.gen.Task(self.s_oauth.udpate_gitlab_token,user_id,{
          "access_token":access_token,
          "token_type":token_type,
          "expires_in":expires_in,
          "refresh_token":refresh_token
        })
        
        if user is None:
            write_result(data=user)
        else:
            render_error(error_code=404,msg='not user data')
            


@@ -268,16 +268,150 @@
 eHandler
+%0Aimport libcurl%0Aimport pycurl%0Aimport tornado.curl_httpclient%0A%0AAsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
 %0A%0Aclass 
@@ -696,33 +696,24 @@
      return%0A
-        %0A
         user
@@ -822,32 +822,123 @@
         
-%0A        if user
+# %E4%BD%BF%E7%94%A8http client%E8%AF%B7%E6%B1%82token%0A        respose = yield tornado.gen.Task(self.request_token,code)%0A        if respose
  is None
@@ -961,36 +961,39 @@
 ite_result(data=
-u
+respo
 se
-r
 )%0A        else:%0A
@@ -1055,17 +1055,267 @@
 ata')%0A  
-%0A
+  %0A    @tornado.gen.engine%0A    def request_token(self,code,callback=None):%0A        http_client = AsyncHTTPClient()%0A        response = yield gen.Task(http_client.fetch, %22http://example.com%22,method=POST, body=json.dumps(data))%0A        callback(response)
 %0A%0Aclass 

Update oauth.py