def add_parameter(self, name, type, checker=None, kind='query', required=True, defaultValue=None, description=''): encoder = converter.encoders[self.encoding if kind == 'body' else converter.application.text] p = parameter.SwaggerParameter(self.pos, name, required, defaultValue, description, checker, encoder, type, self) self.parameters.append(p) self.pos += 1 if kind == 'query': self.query_parameters.append(p) elif kind == 'path': self.path_parameters.append(p) elif kind == 'header': self.header_parameters.append(p) elif kind == 'cookie': self.cookie_parameters.append(p) elif kind == 'body': self.body_parameter = p else: raise errors.InvalidSwaggerDefinition() return p
def load(self, details): self.encoding = details.get('consumes', [converter.application.text])[0] for name, detail3 in details.get('responses', {}).items(): self.responses[name] = detail3.get('description', '') if name == '200': self.return_type, self.array = swagger.get_type(detail3) for detail3 in details.get('parameters', []): k = detail3['in'] name = detail3['name'] encoding = self.encoding if k == 'body' else converter.application.text p = create_parameter(self, detail3, self.pos, name, encoding=encoding) self.parameters.append(p) if k == 'body': self.body_parameter = p elif k == 'query': self.query_parameters.append(p) elif k == 'path': self.path_parameters.append(p) elif k == 'header': self.header_parameters.append(p) elif k == 'cookie': self.cookie_parameters.append(p) elif k == 'formData': self.body_parameter = p else: raise errors.InvalidSwaggerDefinition() self.pos += 1
def set_name(self, v): if self.name is None: self.name = v elif self.name != v: raise errors.InvalidSwaggerDefinition(parameter=self.name, value=v, status='conflict')
def set_type(self, v): if self.type is None: self.type = v if v == common.pandas_df_type: self.array = True if issubclass(v, Enum): self.enum = list(v) elif self.type != v: raise errors.InvalidSwaggerDefinition(reponse='return', value=v)
def set_default(self, v): if self.defaultValue is None: self.defaultValue = v self.required = False elif self.defaultValue != v: raise errors.InvalidSwaggerDefinition(parameter=self.name, default=self.defaultValue, value=v, status='conflict')
def create_checker(parent, details): s = details.get('schema', details) if '$ref' in s: name = s['$ref'] if name[:14] == '#/definitions/': name = name[14:] if name in parent.definitions: return parent.definitions[name] return forward_checker(parent, name) raise errors.InvalidSwaggerDefinition(ref=name, status='unknown') f = s.get('format', None) t = s.get('type', None) if t == 'string': if 'enum' in details: c = enum_checker(s['enum']) elif f == 'date': c = type_checker(datetime.date) elif f == 'date-time': c = type_checker(datetime.date, datetime.datetime) else: c = type_checker(str) elif t == 'number': c = type_checker(float, int) elif t == 'boolean': c = type_checker(bool) elif t == 'integer' or t == 'long': c = type_checker(int) elif t == 'array': items = s.get('items', {}) c = array_checker(create_checker(parent, items)) elif t == 'file': c = type_checker(str) elif t == 'object': mandatory = s != details and s.get('required', []) or [] fields = s.get('properties', {}) c = dict_checker(dict((k, create_checker(parent, v)) for k, v in fields.items()), set(mandatory)) elif t in parent.definitions: c = parent.definitions[t] else: raise errors.InvalidSwaggerDefinition(type=t, status='unknown') return c