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

Commit 3b36ef0

Browse files
authored
Merge pull request #103 from core-api/add-format-argument
Add format argument
2 parents f3fdbb7 + 0f7db6c commit 3b36ef0

File tree

8 files changed

+22
-7
lines changed

8 files changed

+22
-7
lines changed

coreapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from coreapi.document import Array, Document, Link, Object, Error, Field
55

66

7-
__version__ = '2.0.7'
7+
__version__ = '2.0.8'
88
__all__ = [
99
'Array', 'Document', 'Link', 'Object', 'Error', 'Field',
1010
'Client',

coreapi/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,22 @@ def decoders(self):
112112
def transports(self):
113113
return self._transports
114114

115-
def get(self, url, force_codec=False):
115+
def get(self, url, format=None, force_codec=False):
116116
link = Link(url, action='get')
117117

118+
decoders = self.decoders
119+
if format:
120+
decoders = [decoder for decoder in self.decoders if decoder.format == format]
121+
if not decoders:
122+
raise ValueError("No decoder available with format='%s'" % format)
123+
118124
# Perform the action, and return a new document.
119125
transport = determine_transport(self.transports, link.url)
120-
return transport.transition(link, self.decoders, force_codec=force_codec)
126+
return transport.transition(link, decoders, force_codec=force_codec)
121127

122-
def reload(self, document, force_codec=False):
128+
def reload(self, document, format=None, force_codec=False):
123129
# Fallback for v1.x. To be removed in favour of explict `get` style.
124-
return self.get(document.url, force_codec=force_codec)
130+
return self.get(document.url, format=format, force_codec=force_codec)
125131

126132
def action(self, document, keys, params=None, validate=True, overrides=None,
127133
action=None, encoding=None, transform=None):

coreapi/codecs/corejson.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def _primative_to_document(data, base_url=None):
231231

232232
class CoreJSONCodec(BaseCodec):
233233
media_type = 'application/coreapi+json'
234+
format = 'corejson'
234235

235236
# The following is due to be deprecated...
236237
media_types = ['application/coreapi+json', 'application/vnd.coreapi+json']

coreapi/codecs/download.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class DownloadCodec(BaseCodec):
100100
A codec to handle raw file downloads, such as images and other media.
101101
"""
102102
media_type = '*/*'
103+
format = 'download'
103104

104105
def __init__(self, download_dir=None):
105106
"""

coreapi/codecs/jsondata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class JSONCodec(BaseCodec):
99
media_type = 'application/json'
10+
format = 'json'
1011

1112
def decode(self, bytestring, **options):
1213
"""

coreapi/codecs/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class TextCodec(BaseCodec):
66
media_type = 'text/*'
7+
format = 'text'
78

89
def decode(self, bytestring, **options):
910
return bytestring.decode('utf-8')

docs/api-guide/client.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Make a network request to the given URL. If fetching an API schema or hypermedia
7474
resource, then this should typically return a decoded `Document`.
7575

7676
* `url` - The URL that should be retrieved.
77+
* `format` - Optional. Force the given codec to be used when decoding the response.
7778

7879
For example:
7980

docs/api-guide/codecs.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The following attribute is available on codec instances:
6464
Supports decoding or encoding the Core JSON format.
6565

6666
**.media_type**: `application/coreapi+json`
67+
**.format**: `openapi`
6768

6869
Example of decoding a Core JSON bytestring into a `Document` instance:
6970

@@ -99,6 +100,7 @@ URLs in the document.
99100
Supports decoding JSON data.
100101

101102
**.media_type**: `application/json`
103+
**.format**: `json`
102104

103105
Example:
104106

@@ -116,6 +118,7 @@ Example:
116118
Supports decoding plain-text responses.
117119

118120
**.media_type**: `text/*`
121+
**.format**: `text`
119122

120123
Example:
121124

@@ -133,6 +136,7 @@ Supports decoding arbitrary media as a download file. Returns a [temporary file]
133136
that will be deleted once it goes out of scope.
134137

135138
**.media_type**: `*/*`
139+
**.format**: `download`
136140

137141
Example:
138142

@@ -183,7 +187,7 @@ indicate the download filename][content-disposition-filename].
183187
## Custom codecs
184188

185189
Custom codec classes may be created by inheriting from `BaseCodec`, setting
186-
the `media_type` and `supports` properties, and implementing one or both
190+
the `media_type` and `format` properties, and implementing one or both
187191
of the `decode` or `encode` methods.
188192

189193
For example:
@@ -193,7 +197,7 @@ For example:
193197

194198
class YAMLCodec(codecs.BaseCodec):
195199
media_type = 'application/yaml'
196-
supports = ['data']
200+
format = 'yaml'
197201

198202
def decode(content, **options):
199203
return yaml.safe_load(content)

0 commit comments

Comments
 (0)