8000 Version 3 by tomchristie · Pull Request #156 · core-api/python-client · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Version 3 #156

Open
wants to merge 61 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Select commit Hold shift + click to select a range
9bc4fbb
Drop 'transform', and remove 'inplace' transformations.
tomchristie Jan 29, 2018
30e4cb1
Merge branch 'master' into remove-transform
tomchristie Jan 30, 2018
b9f9dd7
Drop action= and encoding= parameters on client, in favor of override
tomchristie Jan 30, 2018
4fb5e88
Drop HTTPTransport 'credentials' in favor of 'auth'
tomchristie Jan 30, 2018
b0eb95c
Drop HTTP request_callback and response_callback in favor of custom s…
tomchristie Jan 30, 2018
ad00b0a
Drop unused import
tomchristie Jan 30, 2018
15dcdc9
Removing data from documents. Drop 'Array'.
tomchristie Jan 30, 2018
f3c8dde
Drop CallbackAdapater. We don't use it anywhere. Could later farm it …
tomchristie Jan 31, 2018
7d06d93
Drop DomainCredentials, now that we have proper auth classes
tomchristie Jan 31, 2018
ced53cf
Refactor to use session.request
tomchristie Jan 31, 2018
93946cd
Drop unneccessary default parameters
tomchristie Jan 31, 2018
e99f71b
Move _guess_extension out of utils and into codes/download
tomchristie Jan 31, 2018
61d4198
Drop 'Document.clone'
tomchristie Feb 13, 2018
bc6aa9e
Add typesys. Add OpenAPI, JSONSchema.
tomchristie Feb 14, 2018
df86966
.errors always a classmethod
tomchristie Feb 14, 2018
f106bd0
Create absolute URLs for Links from OpenAPI
tomchristie Feb 14, 2018
6b3296a
Drop coreschema
tomchristie Feb 14, 2018
8644689
Ordered schema representations. Support OpenAPI encoding.
tomchristie Feb 14, 2018
325d032
Include yaml in requirements
tomchristie Feb 14, 2018
5984a33
Drop coreschema
tomchristie Feb 14, 2018
9444bc0
Preserve ordering of OpenAPI 'paths' across python versions
tomchristie Feb 14, 2018
b3fa5c2
Default to JSON-flavoured OpenAPI
tomchristie Feb 15, 2018
235f50e
Schemas in tests should be bytestrings
tomchristie Feb 15, 2018
dc4edea
Ensure OpenAPI.encode returns bytestrings
tomchristie Feb 15, 2018
c85818c
dict is ordered in Python 3.6
tomchristie Feb 15, 2018
566abbe
Force ordering of document using in test case
tomchristie Feb 15, 2018
6530dc2
Enforced ordering in Object
tomchristie Feb 20, 2018
09518cd
Drop commented-out YAML code
tomchristie Feb 20, 2018
73c5015
Drop itypes from transports/codecs/client
tomchristie Feb 20, 2018
2d6e0bc
Drop BaseCodec dump/load/supports
tomchristie Feb 20, 2018
48784f4
Add document.version
tomchristie Feb 20, 2018
5e19106
Drop itypes from Error
tomchristie Feb 20, 2018
d1c006f
Drop itypes from Link
tomchristie Feb 20, 2018
b52553e
Drop itypes
tomchristie Feb 20, 2018
b1e3418
Drop client.reload
tomchristie Feb 20, 2018
7b0cf7d
_to_immutable -> _to_objects
tomchristie Feb 20, 2018
e663b9f
Drop reload from tests
tomchristie Feb 20, 2018
f09ca74
'enum' defined on types
tomchristie Feb 20, 2018
93482e9
Add openapi codec to setup
tomchristie Feb 20, 2018
54199e3
Add Field.title
tomchristie Feb 20, 2018
8c1748e
Use Link.method. Put .action towards deprecation.
tomchristie Feb 20, 2018
ae79ef5
Plain old classes for typesys
tomchristie Feb 21, 2018
01a0a51
Add typesys.Ref
tomchristie Feb 21, 2018
46a9559
assert types on __init__
tomchristie Feb 21, 2018
f772992
Tests and refinements to typesys
tomchristie Feb 21, 2018
a50e0e3
exact_items validation for Array
tomchristie Feb 21, 2018
b56b1d6
Add allow_null to typesys
tomchristie Feb 21, 2018
c58ad81
Work on JSON Schema and typesys
tomchristie Mar 1, 2018
15f24dc
Work on Union and JSONSchema
tomchristie Mar 1, 2018
cb8e273
Cleanups on typesys
tomchristie Mar 1, 2018
f19c680
Uniqueness checking always that always uses set()
tomchristie Mar 5, 2018
a87f66a
Add Section, Add Link.id
tomchristie Mar 5, 2018
382eef8
Slugify tag names to get section ids
tomchristie Mar 5, 2018
4a5993a
Add path_links and query_links to Section
tomchristie Mar 6, 2018
5e3de6c
Add path_fields and query_fields to Link
tomchristie Mar 6, 2018
1d7a121
Tweak
tomchristie Mar 6, 2018
58ec296
Fix 'security' in OpenAPI
tomchristie Mar 6, 2018
947e03b
If no operationId then use summary for Link.id
tomchristie Mar 6, 2018
7486932
Building out OpenAPI v3 spec
tomchristie Mar 6, 2018
69a5a69
First pass at parsing OpenAPI requestBody schemas
tomchristie Mar 6, 2018
3a793b9
Add initial requestBody schema support
tomchristie Mar 6, 2018
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
Prev Previous commit
Next Next commit
Default to JSON-flavoured OpenAPI
  • Loading branch information
tomchristie committed Feb 15, 2018
commit b3fa5c2b89c0046c86e8f09af4e6bad4fadf958d
4 changes: 2 additions & 2 deletions coreapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# coding: utf-8
from coreapi import auth, codecs, exceptions, transports, typesys, utils
from coreapi.client import Client
from coreapi.document import Document, Link, Object, Error, Field
from coreapi.document import Document, Link, Object, Error, Field, Array


__version__ = '3.0.0'
__all__ = [
'Document', 'Link', 'Object', 'Error', 'Field',
'Document', 'Link', 'Object', 'Error', 'Field', 'Array',
'Client',
'auth', 'codecs', 'exceptions', 'transports', 'typesys', 'utils',
]
138 changes: 76 additions & 62 deletions coreapi/codecs/openapi.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,76 @@
from coreapi.codecs import BaseCodec, JSONSchemaCodec
from coreapi.compat import dict_type, urlparse
from coreapi.compat import VERBOSE_SEPARATORS, dict_type, urlparse
from coreapi.document import Document, Link, Field
from coreapi.exceptions import ParseError
from coreapi.schemas import OpenAPI
import yaml
import json


METHODS = [
'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'
]


def represent_odict(dump, tag, mapping, flow_style=None):
"""Like BaseRepresenter.represent_mapping, but does not issue the sort().
"""
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump.represent_data(item_key)
node_value = dump.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
best_style = False
value.append((node_key, node_value))
if flow_style is None:
if dump.default_flow_style is not None:
node.flow_style = dump.default_flow_style
else:
node.flow_style = best_style
return node


def represent_list(dump, tag, sequence, flow_style=None):
value = []
node = yaml.SequenceNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
for item in sequence:
node_item = dump.represent_data(item)
if not (isinstance(node_item, yaml.ScalarNode) and not node_item.style):
best_style = False
value.append(node_item)
if flow_style is None:
if dump.default_flow_style is not None:
node.flow_style = dump.default_flow_style
else:
node.flow_style = best_style
return node


class CustomSafeDumper(yaml.SafeDumper):
def increase_indent(self, flow=False, indentless=False):
return super(CustomSafeDumper, self).increase_indent(flow, False)


CustomSafeDumper.add_multi_representer(
dict_type,
lambda dumper, value: represent_odict(dumper, u'tag:yaml.org,2002:map', value)
)
CustomSafeDumper.add_multi_representer(
list,
lambda dumper, value: represent_list(dumper, u'tag:yaml.org,2002:seq', value)
)
# def represent_odict(dump, tag, mapping, flow_style=None):
# """Like BaseRepresenter.represent_mapping, but does not issue the sort().
# """
# value = []
# node = yaml.MappingNode(tag, value, flow_style=flow_style)
# if dump.alias_key is not None:
# dump.represented_objects[dump.alias_key] = node
# best_style = True
# if hasattr(mapping, 'items'):
# mapping = mapping.items()
# for item_key, item_value in mapping:
# node_key = dump.represent_data(item_key)
# node_value = dump.represent_data(item_value)
# if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
# best_style = False
# if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
# best_style = False
# value.append((node_key, node_value))
# if flow_style is None:
# if dump.default_flow_style is not None:
# node.flow_style = dump.default_flow_style
# else:
# node.flow_style = best_style
# return node
#
#
# def represent_list(dump, tag, sequence, flow_style=None):
# value = []
# node = yaml.SequenceNode(tag, value, flow_style=flow_style)
# if dump.alias_key is not None:
# dump.represented_objects[dump.alias_key] = node
# best_style = True
# for item in sequence:
# node_item = dump.represent_data(item)
# if not (isinstance(node_item, yaml.ScalarNode) and not node_item.style):
# best_style = False
# value.append(node_item)
# if flow_style is None:
# if dump.default_flow_style is not None:
# node.flow_style = dump.default_flow_style
# else:
# node.flow_style = best_style
# return node
#
#
# class CustomSafeDumper(yaml.SafeDumper):
# def increase_indent(self, flow=False, indentless=False):
# return super(CustomSafeDumper, self).increase_indent(flow, False)
#
#
# CustomSafeDumper.add_multi_representer(
# dict_type,
# lambda dumper, value: represent_odict(dumper, u'tag:yaml.org,2002:map', value)
# )
# CustomSafeDumper.add_multi_representer(
# list,
# lambda dumper, value: represent_list(dumper, u'tag:yaml.org,2002:seq', value)
# )
#
# return yaml.dump(openapi, Dumper=CustomSafeDumper, default_flow_style=False)


def _relative_url(base_url, url):
Expand All @@ -89,7 +92,11 @@ class OpenAPICodec(BaseCodec):
format = 'openapi'

def decode(self, bytestring, **options):
data = yaml.safe_load(bytestring)
try:
data = json.loads(bytestring.decode('utf-8'))
except ValueError as exc:
raise ParseError('Malformed JSON. %s' % exc)

openapi = OpenAPI(data)
title = openapi.lookup(['info', 'title'])
description = openapi.lookup(['info', 'description'])
Expand Down Expand Up @@ -190,7 +197,14 @@ def encode(self, document, **options):
}],
'paths': paths
})
return yaml.dump(openapi, Dumper=CustomSafeDumper, default_flow_style=False)

kwargs = {
'ensure_ascii': False,
'indent': 4,
'separators': VERBOSE_SEPARATORS
}

return json.dumps(openapi, **kwargs)

def get_paths(self, document):
paths = dict_type()
Expand Down
31 changes: 31 additions & 0 deletions coreapi/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ def b64encode(input_string):
return base64.b64encode(input_string.encode('ascii')).decode('ascii')


try:
import coreschema
except ImportError:
# Temporary shim, to support 'coreschema' until it's fully deprecated.
def coreschema_to_typesys(item):
return item
else:
def coreschema_to_typesys(item):
from coreapi import typesys

# We were only ever using the type and title/description,
# so we don't both to include the full set of keyword arguments here.
if isinstance(item, coreschema.String):
return typesys.string(title=item.title, description=item.description)
elif isinstance(item, coreschema.Integer):
return typesys.integer(title=item.title, description=item.description)
elif isinstance(item, coreschema.Number):
return typesys.number(title=item.title, description=item.description)
elif isinstance(item, coreschema.Boolean):
return typesys.boolean(title=item.title, description=item.description)
elif isinstance(item, coreschema.Enum):
return typesys.enum(title=item.title, description=item.description, enum=item.enum)
elif isinstance(item, coreschema.Array):
return typesys.array(title=item.title, description=item.description)
elif isinstance(item, coreschema.Object):
return typesys.obj(title=item.title, description=item.description)
elif isinstance(item, coreschema.Anything):
return typesys.any(title=item.title, description=item.description)

return item

def force_bytes(string):
if isinstance(string, string_types):
return string.encode('utf-8')
Expand Down
9 changes: 7 additions & 2 deletions coreapi/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8
from __future__ import unicode_literals
from collections import OrderedDict
from coreapi.compat import string_types
from coreapi.compat import coreschema_to_typesys, string_types
import itypes


Expand Down Expand Up @@ -235,7 +235,7 @@ def __init__(self, name, required=False, location='', schema=None, description=N
self.name = name
self.required = required
self.location = location
self.schema = schema
self.schema = coreschema_to_typesys(schema)
self.description = description
self.example = example

Expand Down Expand Up @@ -296,3 +296,8 @@ def get_messages(self):
elif isinstance(value, string_types):
messages += [value]
return messages


class Array(object):
def __init__(self):
assert False, 'Array is deprecated'
4 changes: 2 additions & 2 deletions coreapi/typesys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from coreapi.compat import dict_type
from coreapi.compat import dict_type, string_types
import re
# from typing import Any, Dict, List, Optional, Tuple, Union, overload # noqa

Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(self, value):

# Ensure all property keys are strings.
errors = {}
if any(not isinstance(key, str) for key in value.keys()):
if any(not isinstance(key, string_types) for key in value.keys()):
self.error('invalid_key')

# Properties
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
itypes
requests
uritemplate
pyyaml

# Testing requirements
coverage
Expand Down
Loading
0