Skip to content

Commit

Permalink
Add file support for both python2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
keithwhor committed Jul 15, 2017
1 parent a72b106 commit 031da27
Showing 1 changed file with 75 additions and 11 deletions.
86 changes: 75 additions & 11 deletions lib/__init__.py
Expand Up @@ -91,7 +91,6 @@ def __getattr__(self, name):
def __call__(self, *args, **kwargs):

import sys
import json
import urllib

cfg = self.__cfg__
Expand All @@ -115,11 +114,6 @@ def __call__(self, *args, **kwargs):
elif bool(kwargs):
raise ValueError('.'.join(self.__names__) + ': Can not pass arguments and kwargs')

if bool(kwargs):
body = json.dumps(kwargs)
else:
body = json.dumps(args)

headers = {}
headers['Content-Type'] = 'application/json'
headers['X-Faaslang'] = 'true'
Expand All @@ -136,12 +130,83 @@ def __call__(self, *args, **kwargs):
pathname += '={0}'.format(bg)

if (sys.version_info > (3, 0)):
return self.__py3__(cfg, pathname, body, headers)
body = self.__py3body__(args, kwargs)
return self.__py3request__(cfg, pathname, body, headers)
else:
return self.__py2__(cfg, pathname, body, headers)
body = self.__py2body__(args, kwargs)
return self.__py2request__(cfg, pathname, body, headers)

def __py3body__(self, args, kwargs):

import base64
import json
import io

if bool(kwargs):
kw = {}
for key in kwargs:
value = kwargs[key]
if isinstance(value, io.IOBase):
if value.closed:
raise ValueError('.'.join(self.__names__) + ': Can not read from closed file')
if value.encoding == 'UTF-8':
value = value.buffer
base64data = base64.b64encode(value.read()).decode('UTF-8')
value.close()
kw[key] = {'_base64': base64data}
else:
kw[key] = value
body = json.dumps(kw)
else:
a = []
for value in args:
if isinstance(value, io.IOBase):
if value.closed:
raise ValueError('.'.join(self.__names__) + ': Can not read from closed file')
if value.encoding == 'UTF-8':
value = value.buffer
base64data = base64.b64encode(value.read()).decode('UTF-8')
value.close()
a.push({'_base64': base64data})
else:
a.push(value)
body = json.dumps(args)

def __py3__(self, cfg, pathname, body, headers):
return body

def __py2body__(self, args, kwargs):

import json

if bool(kwargs):
kw = {}
for key in kwargs:
value = kwargs[key]
if isinstance(value, file):
if value.closed:
raise ValueError('.'.join(self.__names__) + ': Can not read from closed file')
base64data = value.read().encode('base64').strip()
value.close()
kw[key] = {'_base64': base64data}
else:
kw[key] = value
body = json.dumps(kw)
else:
a = []
for value in args:
if isinstance(value, file):
if value.closed:
raise ValueError('.'.join(self.__names__) + ': Can not read from closed file')
base64data = value.read().encode('base64').strip()
value.close()
a.push({'_base64': base64data})
else:
a.push(value)
body = json.dumps(args)

return body

def __py3request__(self, cfg, pathname, body, headers):

import http.client

Expand All @@ -154,8 +219,7 @@ def __py3__(self, cfg, pathname, body, headers):

return self.__complete__(r.status, contentType, response)


def __py2__(self, cfg, pathname, body, headers):
def __py2request__(self, cfg, pathname, body, headers):

import httplib

Expand Down

0 comments on commit 031da27

Please sign in to comment.