Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.md
include LICENSE
13 changes: 5 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,25 @@
from distutils.core import setup

setup(
name='smsapi',
name='smsapi-pozytywnie',
version=smsapi.__version__,
description='Python library for manage SmsApi account via HTTP/S',
long_description=open('README.md').read(),
author='SMSAPI',
author_email='bok@smsapi.pl',
url='https://github.com/smsapi/python-client',
url='https://github.com/Kern3l/smsapi-python-client',
packages=['smsapi', 'smsapi.actions'],
package_data={'': ['LICENSE', 'NOTICE'], 'requests': ['*.pem']},
package_dir={'smsapi': 'smsapi'},
include_package_data=True,
license=open('LICENSE').read(),
classifiers=(
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: Apache 2.0',
'Topic :: Communications :: Mobile messages'
'License :: OSI Approved :: Apache Software License',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules'
'Topic :: Software Development :: Libraries :: Python Modules',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
),
test_suite='tests.suite'
)
)
2 changes: 1 addition & 1 deletion smsapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

__title__ = 'SmsApi'
__version__ = '1.0_beta'
__version__ = '1.0_beta3'
__author__ = 'SMSAPI'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2013 SMSAPI'
58 changes: 29 additions & 29 deletions smsapi/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from mimetools import choose_boundary
except ImportError:
from uuid import uuid4

def choose_boundary():
return str(uuid4())


if sys.version_info[0] == 3:
text_type = str
Expand All @@ -33,7 +33,7 @@ class ApiProxyError(Exception):


class ApiProxy(object):

def __init__(self, hostname=None, data=None):
super(ApiProxy, self).__init__()

Expand All @@ -51,50 +51,50 @@ def execute(self):


class ApiHttpProxy(ApiProxy):

user_agent = 'PySmsAPI'

def __init__(self, hostname=None, data=None):
super(ApiHttpProxy, self).__init__(hostname, data)

self.headers = {}

self.body = {}

def execute(self, uri=None, data=None):

if isinstance(data, dict):
self.data.update(data)
self.data.update(data)

headers, body = self.prepare_request()

response = None

if isinstance(self.hostname, (list, tuple)):
for host in self.hostname:
response = self.connect(host, uri, body, headers)

if response and response.getcode() == 200:
break
else:
response = self.connect(self.hostname, uri, body, headers)

if not response:
raise ApiProxyError("Unable connect to the specified url: %s" % str(self.hostname))

return response

def connect(self, hostname, uri, body, headers):
try:
uri = uri or ''
if hostname.endswith('/'):
url = hostname + uri
else:
url = '%s/%s' % (hostname, uri)
url = '%s/%s' % (hostname, uri)

req = Request(url, body, headers)
req = Request(url, body.encode('UTF-8'), headers)
response = urlopen(req)

return response
except (URLError, ValueError):
return False
Expand All @@ -104,30 +104,30 @@ def add_file(self, filepath):
self.files.append(filepath)
else:
raise ValueError('Argument must be a file.')

def prepare_request(self):

headers = {
'User-Agent': self.user_agent,
}

if isinstance(self.data, dict):
self.data.update(self.data)
self.data.update(self.data)

if self.files:
content_type, data = self.encode_multipart_data()

headers.update({
'Content-Type': content_type,
'Content-Type': content_type,
'Content-Length': str(len(data))
})
else:
headers.update({
'Content-type': "application/x-www-form-urlencoded; charset=utf-8"
})

data = urlencode(self.data)

return headers, data

def encode_multipart_data(self):
Expand All @@ -136,30 +136,30 @@ def encode(data):
if isinstance(data, text_type):
data = data.encode('utf-8')
return data

boundary = choose_boundary()

body = BytesIO()

for (key, value) in self.data.items():
body.write(encode('--%s\r\n' % boundary))
body.write(encode('Content-Disposition: form-data; name="%s"' % key))
body.write(encode('\r\n\r\n' + value + '\r\n'))

for _file in self.files:
body.write(encode('--%s\r\n' % boundary))
body.write(encode('Content-Disposition: form-data; name="file"; filename="%s"\r\n' % _file))
body.write(encode('Content-Type: %s\r\n' % mimetypes.guess_type(_file)[0] or 'application/octet-stream'))
body.write(encode('\r\n'))

try:
with open(_file, 'rb') as f:
data = f.read()
body.write(encode(data))
except IOError:
raise
body.write(encode('\r\n'))

body.write(encode('\r\n'))

body.write(encode('--%s--\r\n\r\n' % boundary))

Expand Down