Commit f917b408 authored by 张宇's avatar 张宇

[M]HuanXinClient singleton

parent 1b1f4ca5
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json import json
from typing import Optional from typing import Optional
from threading import Lock
from django.conf import settings from django.conf import settings
import requests import requests
...@@ -39,6 +40,9 @@ def handle_huanxin_response_error(response: requests.models.Response, client=Non ...@@ -39,6 +40,9 @@ def handle_huanxin_response_error(response: requests.models.Response, client=Non
return result return result
_instance = None
_lock = Lock()
class HuanXinClient(object): class HuanXinClient(object):
BASE_URL = getattr(settings, 'HX_BASE_URL', None) BASE_URL = getattr(settings, 'HX_BASE_URL', None)
ORG_NAME = getattr(settings, 'HX_ORG_NAME', None) ORG_NAME = getattr(settings, 'HX_ORG_NAME', None)
...@@ -54,6 +58,14 @@ class HuanXinClient(object): ...@@ -54,6 +58,14 @@ class HuanXinClient(object):
'huanxin config error' 'huanxin config error'
self.session = requests.Session() self.session = requests.Session()
@staticmethod
def __new__(cls, *args, **kwargs):
global _lock, _instance
_lock.acquire()
_instance = _instance or super(HuanXinClient, cls).__new__(cls)
_lock.release()
return _instance
def _get_auth_token(self): def _get_auth_token(self):
GET_TOKEN_URL = '{base_url}/{org_name}/{app_name}/token'.format( GET_TOKEN_URL = '{base_url}/{org_name}/{app_name}/token'.format(
base_url=self.BASE_URL, base_url=self.BASE_URL,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment