Skip to content

Commit

Permalink
Merge pull request #17 from jeronimoalbi/develop
Browse files Browse the repository at this point in the history
Update to 1.0.0-alpha.15
  • Loading branch information
nullproxy authored Oct 12, 2016
2 parents b8fee70 + 6b0e983 commit 60b3987
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion katana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

__license__ = "MIT"
__copyright__ = "Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)"
__version__ = '1.0.0-alpha.14'
__version__ = '1.0.0-alpha.15'
28 changes: 17 additions & 11 deletions katana/api/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,18 @@ def set_property(self, name, value):
str(value),
)

def get_param(self, location, name):
def get_param(self, name, location='query'):
"""Gets a parameter passed to the action.
Returns a Param object containing the parameter for the given location
and name.
Valid location values are "path", "query", "form-data", "header"
and "body".
Valid location values are "path", "query", "form-data" and "header".
:param location: The parameter location.
:type location: str
:param name: The parameter name.
:type name: str
:param location: The parameter location.
:type location: str
:rtype: `Param`
Expand All @@ -139,23 +138,22 @@ def get_param(self, location, name):
param_path = '{}/{}'.format(location, name)
value = self.__params.get(param_path + '/value', None)
return Param(
location,
name,
location=location,
value=value,
datatype=self.__params.get(param_path + '/type', None),
exists=self.__params.path_exists(param_path),
)

def new_param(self, location, name, value=None, datatype=None):
def new_param(self, name, location='query', value=None, datatype=None):
"""Creates a new parameter object.
Creates an instance of Param with the given location and name, and
optionally the value and data type. If the value is not provided then
an empty string is assumed. If the data type is not defined then
"string" is assumed.
Valid location values are "path", "query", "form-data", "header"
and "body".
Valid location values are "path", "query", "form-data" and "header".
Valid data types are "null", "boolean", "integer", "float", "string",
"array" and "object".
Expand All @@ -178,7 +176,13 @@ def new_param(self, location, name, value=None, datatype=None):
else:
datatype = Param.resolve_type(value)

return Param(location, name, value, datatype, True)
return Param(
name,
location=location,
value=value,
datatype=datatype,
exists=True,
)

def has_file(self, name):
"""Check if a file was provided for the action.
Expand All @@ -198,12 +202,14 @@ def get_file(self, name):
:param name: File name.
:type name: str
:rtype: `File` or None
:rtype: `File`
"""

if self.has_file(name):
return payload_to_file(name, self.__files[name])
else:
return File(name, path='')

def new_file(self, name, path, mime=None):
"""Create a new file.
Expand Down
6 changes: 5 additions & 1 deletion katana/api/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class File(object):
"""

def __init__(self, name, path, **kwargs):
if (not path) or path[:4] not in ('file', 'http'):
# When a path is given check protocol
if path and path[:7] not in ('file://', 'http://'):
raise TypeError('Path must begin with file:// or http://')

self.__name = name
Expand Down Expand Up @@ -151,6 +152,9 @@ def exists(self):
"""

if self.__path:
return False

# Check remote file existence when path is HTTP (otherwise is file://)
if self.__path[:7] == 'http://':
# Setup headers for request
Expand Down
21 changes: 12 additions & 9 deletions katana/api/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ class Param(object):
"""

def __init__(self, location, name, **kwargs):
def __init__(self, name, location='query', **kwargs):
if location not in LOCATIONS:
raise TypeError('Unknown location value')
location = 'query'

self.__value = kwargs.get('value') or ''
self.__location = location
self.__name = name

self.__type = kwargs.get('datatype')
if not self.__type:
if self.__type not in TYPE_CLASSES:
self.__type = TYPE_STRING
elif not self.__type:
self.__type = self.resolve_type(self.__value)

self.__exists = kwargs.get('exists', False)
Expand Down Expand Up @@ -95,23 +98,23 @@ def resolve_type(cls, value):

return TYPE_OBJECT

def get_location(self):
"""Get location where parameter was defined.
def get_name(self):
"""Get aprameter name.
:rtype: str
"""

return self.__location
return self.__name

def get_name(self):
"""Get aprameter name.
def get_location(self):
"""Get location where parameter was defined.
:rtype: str
"""

return self.__name
return self.__location

def get_type(self):
"""Get parameter data type.
Expand Down
9 changes: 6 additions & 3 deletions katana/api/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..utils import MultiDict

from .base import Api
from .file import File
from .response import Response
from .transport import Transport

Expand Down Expand Up @@ -376,9 +377,11 @@ def get_file(self, name):
"""

# Get only the first file.
# Note: Multiple files can be uploaded to gateway for the same name.
return self.__files.getone(name)
if name in self.__files:
# Get only the first file
return self.__files.getone(name)
else:
return File(name, path='')

def get_files(self):
"""Get uploaded files.
Expand Down
6 changes: 3 additions & 3 deletions katana/api/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def get_request_timestamp(self):

return self.__transport.get('meta/datetime')

def get_origin(self):
"""Get transport origin.
def get_origin_service(self):
"""Get transport origin service.
Origin is a tuple with origin name and version.
Service origin is a tuple with origin name and version.
:rtype: list
Expand Down
4 changes: 2 additions & 2 deletions katana/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
'token': 't',
'total': 't',
'transactions': 't',
'transport': 't',
'type': 'T',
'type': 't',
'transport': 'T',
'url': 'u',
'used': 'u',
'user': 'u',
Expand Down
25 changes: 14 additions & 11 deletions katana/service/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,25 @@ def get_response_meta(self, payload):
if calls:
meta += SERVICE_CALL

# Skip call files check when files flag is already in meta
# TODO: Check for file paths to be sure flag is added when local
# files are used in any call.
# Check if there are files added to calls
if FILES not in meta:
# Add meta for files only when service calls are made.
# Files are setted in a service ONLY when a call to
# another service is made.
files = get_path(transport, 'files', None)
for call in calls:
files_path = '{}/{}/{}'.format(
get_path(call, 'name'),
get_path(call, 'version'),
get_path(call, 'action'),
)
# Add flag and exit when at least one call has files
if path_exists(files, files_path):
meta += FILES
break
if files:
for call in calls:
files_path = '{}/{}/{}'.format(
get_path(call, 'name'),
get_path(call, 'version'),
get_path(call, 'action'),
)
# Add flag and exit when at least one call has files
if path_exists(files, files_path):
meta += FILES
break

return meta

Expand Down

0 comments on commit 60b3987

Please sign in to comment.