50b15762 by 周伟奇

ecm download

1 parent 5a19df38
...@@ -24,3 +24,7 @@ class GCAPException(Exception): ...@@ -24,3 +24,7 @@ class GCAPException(Exception):
24 24
25 class CMSException(Exception): 25 class CMSException(Exception):
26 pass 26 pass
27
28
29 class ECMException(Exception):
30 pass
......
1 import base64
2 import requests
3 from common.redis_cache import redis_handler as rh
4 from settings import conf
5 from apps.doc.exceptions import ECMException
6
7
8 class ECM:
9
10 def __init__(self):
11 self.oauth_token = None
12 self.username = conf.ECM_USER
13 self.pwd = conf.ECM_PWD
14 self.oauth_url = conf.ECM_OAUTH_URL
15 self.download_url = conf.ECM_DOWNLOAD_URL
16 self.oauth_headers = {
17 'Content-Type': 'application/x-www-form-urlencoded'
18 }
19 self.oauth_payload = {
20 'grant_type': 'client_credentials',
21 'client_id': conf.ECM_OAUTH_ID,
22 'client_secret': conf.ECM_OAUTH_SECRET,
23 }
24 self.token_key = 'access_token'
25 self.token_type = 'Bearer'
26 self.token_type_key = 'token_type'
27 self.expires_key = 'expires_in'
28
29 def update_oauth_token(self):
30 response = requests.post(self.oauth_url, headers=self.oauth_headers, data=self.oauth_payload, verify=False)
31 if response.status_code != 200:
32 raise ECMException('ECM Oauth response with code: {0}'.format(response.status_code))
33 token = response.json().get(self.token_key)
34 if not isinstance(token, str):
35 raise ECMException('ECM Oauth can not get token: {0}'.format(response.json()))
36 self.oauth_token = token
37 self.token_type = response.json().get(self.token_type_key, self.token_type)
38 expires = response.json().get(self.expires_key, 3600)
39 rh.set_ecm_token(self.oauth_token, expires)
40
41 def get_oauth_token(self):
42 if self.oauth_token is None:
43 # redis获取token
44 self.oauth_token = rh.get_ecm_token()
45 if self.oauth_token is None:
46 self.update_oauth_token()
47 return self.oauth_token
48
49 def download(self, save_path, doc_base, doc_type, object_id):
50 download_headers = {
51 'Authorization': '{0} {1}'.format(self.token_type, self.get_oauth_token())
52 }
53 download_json = {
54 "userName": self.username,
55 "password": self.pwd,
56 "docbase": doc_base,
57 "documentType": doc_type,
58 "objectId": object_id,
59 }
60 response = requests.post(self.download_url, headers=download_headers, json=download_json, verify=False)
61 if response.status_code != 200:
62 raise ECMException('ECM download response with code: {0}'.format(response.status_code))
63 base64_data = response.json().get('Envelope', {}).get('Body', {}).get('getResponse', {}).get('return', {}).get(
64 'DataObjects', {}).get('Contents', {}).get('Value')
65 if not isinstance(base64_data, str):
66 raise ECMException('ECM download failed: {0}'.format(response.json()))
67 with open(save_path, "wb") as fh:
68 fh.write(base64.b64decode(base64_data.encode()))
69
70 def upload(self):
71 {
72 "username": "fanliubing",
73 "password": "Bmwecm123456&api1",
74 "docbase": "SF5_CN",
75 "documentType": "acceptance",
76 "object_name": "JoeMyPDF202109061529.pdf",
77 "folder": "/Wholesale/Operations/Audits",
78 "format": "pdf",
79 "b_application_no": "",
80 "file_base64_content": "",
81 }
82
83
...@@ -37,6 +37,7 @@ class RedisHandler: ...@@ -37,6 +37,7 @@ class RedisHandler:
37 self.priority_queue_key = '{0}:priority_queue'.format(self.prefix) 37 self.priority_queue_key = '{0}:priority_queue'.format(self.prefix)
38 self.session_id_key = '{0}:session_id'.format(self.prefix) 38 self.session_id_key = '{0}:session_id'.format(self.prefix)
39 self.cms_token_key = '{0}:cms_token'.format(self.prefix) 39 self.cms_token_key = '{0}:cms_token'.format(self.prefix)
40 self.ecm_token_key = '{0}:ecm_token'.format(self.prefix)
40 41
41 def enqueue(self, tasks, is_priority=False): 42 def enqueue(self, tasks, is_priority=False):
42 # 1 43 # 1
...@@ -64,3 +65,9 @@ class RedisHandler: ...@@ -64,3 +65,9 @@ class RedisHandler:
64 def set_cms_token(self, token, expires=None): 65 def set_cms_token(self, token, expires=None):
65 return self.redis.set(self.cms_token_key, token, expires) 66 return self.redis.set(self.cms_token_key, token, expires)
66 67
68 def get_ecm_token(self):
69 return self.redis.get(self.ecm_token_key)
70
71 def set_ecm_token(self, token, expires=None):
72 return self.redis.set(self.ecm_token_key, token, expires)
73
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!