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

Commit c68c356

Browse files
Finesse imports
1 parent e50aa62 commit c68c356

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

coreapi/__init__.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# coding: utf-8
2-
from coreapi.codecs import BaseCodec, CoreJSONCodec, HALCodec, HTMLCodec, PlainTextCodec, PythonCodec
2+
from coreapi.client import Client
33
from coreapi.document import Array, Document, Link, Object, Error, Field
44
from coreapi.exceptions import ParseError, TransportError, ErrorMessage
5-
from coreapi.history import History
6-
from coreapi.client import Client
7-
from coreapi.transports import BaseTransport, HTTPTransport
5+
from coreapi import codecs, history, transports
86

97

108
__version__ = '1.11.4'
119
__all__ = [
12-
'BaseCodec', 'CoreJSONCodec', 'HALCodec', 'HTMLCodec', 'PlainTextCodec', 'PythonCodec',
13-
'negotiate_encoder', 'negotiate_decoder',
1410
'Array', 'Document', 'Link', 'Object', 'Error', 'Field',
1511
'ParseError', 'NotAcceptable', 'TransportError', 'ErrorMessage',
16-
'BaseTransport', 'HTTPTransport', 'Client', 'History',
17-
'load', 'dump', 'get'
12+
'Client',
13+
'negotiate_encoder', 'negotiate_decoder',
14+
'get', 'action', 'reload', 'load', 'dump',
15+
'codecs', 'history', 'transports'
1816
]
1917

2018

@@ -45,12 +43,9 @@ def reload(document):
4543

4644
def load(bytestring, content_type=None):
4745
client = Client()
48-
codec = client.negotiate_decoder(content_type)
49-
return codec.load(bytestring)
46+
return client.load(bytestring, content_type=content_type)
5047

5148

5249
def dump(document, accept=None, **kwargs):
5350
client = Client()
54-
codec = client.negotiate_encoder(accept)
55-
content = codec.dump(document, **kwargs)
56-
return codec.media_type, content
51+
return client.dump(document, accept=accept, **kwargs)

coreapi/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,20 @@ def action(self, document, keys, params=None, action=None, inplace=None):
189189
# Perform the action, and return a new document.
190190
transport = self.determine_transport(link.url)
191191
return transport.transition(link, params, client=self, link_ancestors=link_ancestors)
192+
193+
def load(self, bytestring, content_type=None):
194+
"""
195+
Given a bytestring and an optional content_type, return the
196+
parsed Document.
197+
"""
198+
codec = self.negotiate_decoder(content_type)
199+
return codec.load(bytestring)
200+
201+
def dump(self, document, accept=None, **kwargs):
202+
"""
203+
Given a document, and an optional accept header, return a two-tuple of
204+
the selected media type and encoded bytestring.
205+
"""
206+
codec = self.negotiate_encoder(accept)
207+
content = codec.dump(document, **kwargs)
208+
return (codec.media_type, content)

coreapi/commandline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def bookmarks_get(name):
442442

443443
def get_history():
444444
if not os.path.isfile(history_path):
445-
return coreapi.History(max_items=20)
445+
return coreapi.history.History(max_items=20)
446446
history_file = open(history_path, 'rb')
447447
bytestring = history_file.read()
448448
history_file.close()

tests/test_transitions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding: utf-8
2-
from coreapi import Document, Link, HTTPTransport, Client
2+
from coreapi import Document, Link, Client
3+
from coreapi.transports import HTTPTransport
34
import pytest
45

56

0 commit comments

Comments
 (0)