Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit e667992

Browse files
committed
resource iamge 구현 완료
1 parent c2617d3 commit e667992

6 files changed

Lines changed: 17 additions & 39 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
coolsms.pyc
1+
*.pyc
22
*/.idea/*
33
*/__pycache__
44
test.py
5+
test.jpg
57 Bytes
Binary file not shown.
102 Bytes
Binary file not shown.

sdk/api/image.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
import sys
5+
import base64
56
sys.path.insert(0, "../../")
67

78
from sdk.coolsms import Coolsms
@@ -53,25 +54,22 @@ def get_image_info(self, image_id):
5354

5455
# @brief upload image ( HTTP Method POST )
5556
# @param string image [required]
56-
# @param string encoding [optional]
5757
# @return JSONobject
58-
def upload_image(self, image, encoding=None):
58+
def upload_image(self, image):
5959
if image == None:
6060
raise CoolsmsSDKException("'image' is required", 201);
6161

6262
params = dict()
6363
params = {'image':image}
64-
if encoding:
65-
params['encoding'] = encoding
64+
params['image_encoding'] = 'base64'
6665

6766
files = {}
68-
6967
try:
70-
with open(params['image'], 'rb') as content_file:
71-
content = content_file.read()
68+
with open(image, 'rb') as content_file:
69+
content = base64.b64encode(content_file.read())
70+
content = content.decode()
7271
except Exception as e:
7372
raise CoolsmsSystemException(e, 399)
74-
7573
files = {'image': {'filename': image, 'content': content}}
7674

7775
response = self.cool.request_post_multipart("upload_image", params, files)

sdk/api/message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import sys
55
import platform
6+
import base64
67
sys.path.insert(0, "../../")
78

89
from sdk.coolsms import Coolsms
@@ -63,11 +64,12 @@ def send(self, params):
6364

6465
try:
6566
with open(params['image'], 'rb') as content_file:
66-
content = content_file.read()
67+
content = base64.b64encode(content_file.read())
68+
content = content.decode()
6769
except Exception as e:
6870
raise CoolsmsSystemException(e, 399)
69-
7071
files = {'image': {'filename': params['image'], 'content': content}}
72+
params['image_encoding'] = 'base64'
7173

7274
# request post multipart-form
7375
response = self.cool.request_post_multipart("send", params, files)

sdk/coolsms.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,12 @@ def request_post_multipart(self, resource, params, files):
154154
selector = "/%s/%s/%s" % (self.api_name, self.api_version, resource)
155155

156156
params = self.set_base_params(params)
157+
157158
content_type, body = self.encode_multipart_formdata(params, files)
158159
conn = HTTPSConnection(host)
159160
conn.putrequest('POST', selector)
160-
conn.putheader('content-type', content_type)
161-
conn.putheader('content-length', str(len(body)))
161+
conn.putheader('Content-type', content_type)
162+
conn.putheader('Content-length', str(len(body)))
162163
conn.putheader('User-Agent', 'sms-python')
163164
conn.endheaders()
164165
conn.send(body)
@@ -188,6 +189,7 @@ def request_post_multipart(self, resource, params, files):
188189
def encode_multipart_formdata(self, params, files):
189190
boundary = str(uuid.uuid1())
190191
crlf = '\r\n'
192+
191193
l = []
192194
for key, value in params.items():
193195
l.append('--' + boundary)
@@ -196,41 +198,16 @@ def encode_multipart_formdata(self, params, files):
196198
l.append(value)
197199

198200
for key, value in files.items():
199-
200-
print(self.get_content_type(value['filename']))
201201
l.append('--' + boundary)
202202
l.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, value['filename']))
203203
l.append('Content-Type: %s' % self.get_content_type(value['filename']))
204-
205-
print(type(value['content']))
206204
l.append('')
207205
l.append(str(value['content']))
208206

209207
l.append('--' + boundary + '--')
210208
l.append('')
211-
212209
body = crlf.join(l).encode('utf-8')
213-
"""
214-
body = body.encode('utf-8')
215-
216-
print("WOW")
217-
for key, value in files.items():
218-
print(self.get_content_type(value['filename']))
219-
image_data = '--' + boundary + crlf
220-
image_data += 'Content-Type: %s' % self.get_content_type(value['filename']) + crlf
221-
image_data += 'Content-Disposition: form-data; name="%s"; filename="%s"' % (key, value['filename']) + crlf
222-
image_data += crlf
223-
image_data = image_data.encode('utf-8')
224-
225-
body += image_data
226-
body += value['content']
227-
228-
end_data = crlf + '--' + boundary + '--' + crlf
229-
end_data += crlf
230-
end_data = end_data.encode('utf-8')
231-
body += end_data
232-
"""
233-
210+
234211
content_type = 'multipart/form-data; boundary=%s' % boundary
235212

236213
return content_type, body

0 commit comments

Comments
 (0)