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

Commit eec37eb

Browse files
committed
Merge branch 'master' of github.com:core-api/python-client
2 parents 54664b4 + 0a4329d commit eec37eb

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

coreapi/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def get(url):
4949
return session.get(url)
5050

5151

52-
def action(document, keys, params=None):
52+
def action(document, keys, params=None, action=None, inplace=None):
5353
session = _default_session
54-
return session.action(document, keys, params)
54+
return session.action(document, keys, params, action=action, inplace=inplace)
5555

5656

5757
def load(bytestring, content_type=None):

coreapi/commandline.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,20 @@ def validate_params(ctx, param, value):
128128
return value
129129

130130

131+
def validate_inplace(ctx, param, value):
132+
if value is None:
133+
return
134+
if value.lower() not in ('true', 'false'):
135+
raise click.BadParameter('"--inplace" needs to be one of "true" or "false"')
136+
return value.lower() == 'true'
137+
138+
131139
@click.command(help='Interact with the current document, given a PATH to a link.')
132140
@click.argument('path', nargs=-1)
133141
@click.option('--param', '-p', multiple=True, callback=validate_params, help='Parameter in the form <field name>=<value>.')
134-
def action(path, param):
142+
@click.option('--action', '-a', help='Set the link action explicitly.', default=None)
143+
@click.option('--inplace', '-i', callback=validate_inplace, help='Set the inplace boolean explicitly.', default=None)
144+
def action(path, param, action, inplace):
135145
if not path:
136146
click.echo('Missing PATH to a link in the document.')
137147
sys.exit(1)
@@ -145,7 +155,7 @@ def action(path, param):
145155

146156
session = get_session()
147157
keys = coerce_key_types(doc, path)
148-
doc = session.action(doc, keys, params=params)
158+
doc = session.action(doc, keys, params=params, action=action, inplace=inplace)
149159
click.echo(dump_to_console(doc))
150160
write_to_store(doc)
151161

coreapi/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def __init__(self, url=None, action=None, inplace=None, fields=None):
173173
raise TypeError("Argument 'action' must be a string.")
174174
if (inplace is not None) and (not isinstance(inplace, bool)):
175175
raise TypeError("Argument 'inplace' must be a boolean.")
176-
if (fields is not None) and (not isinstance(fields, list)):
176+
if (fields is not None) and (not isinstance(fields, (list, tuple))):
177177
raise TypeError("Argument 'fields' must be a list.")
178178
if (fields is not None) and any([
179179
not (isinstance(item, string_types) or isinstance(item, Field))

coreapi/sessions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_accept_header(self):
3838
"""
3939
Return an 'Accept' header for the given codecs.
4040
"""
41-
return ', '.join([codec.media_type for codec in self.codecs])
41+
return ', '.join([codec.media_type for codec in self.decoders])
4242

4343
def negotiate_decoder(self, content_type=None):
4444
"""
@@ -112,7 +112,7 @@ def get(self, url):
112112
link = Link(url, action='get')
113113
return transport.transition(link, session=self)
114114

115-
def action(self, document, keys, params=None):
115+
def action(self, document, keys, params=None, action=None, inplace=None):
116116
if isinstance(keys, string_types):
117117
keys = [keys]
118118

@@ -123,6 +123,12 @@ def action(self, document, keys, params=None):
123123
link, link_ancestors = validate_keys_to_link(document, keys)
124124
validate_parameters(link, params)
125125

126+
if action is not None or inplace is not None:
127+
# Handle any explicit overrides.
128+
action = link.action if (action is None) else action
129+
inplace = link.inplace if (inplace is None) else inplace
130+
link = Link(link.url, action, inplace, link.fields)
131+
126132
# Perform the action, and return a new document.
127133
transport = self.determine_transport(link.url)
128134
return transport.transition(link, params, session=self, link_ancestors=link_ancestors)

coreapi/transport.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ def handle_inplace_replacements(self, document, link, link_ancestors):
105105
* Make an inline replacement and then return the modified document tree.
106106
* Return the new document as-is.
107107
"""
108-
inplace = link.inplace
109-
if inplace is None and link.action.lower() in ('put', 'patch', 'delete'):
110-
inplace = True
108+
if link.inplace is None:
109+
inplace = link.action.lower() in ('put', 'patch', 'delete')
110+
else:
111+
inplace = link.inplace
111112

112113
if inplace:
113114
root = link_ancestors[0].document

tests/test_integration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def test_get_session():
7272
credentials={'example.org': 'abc'},
7373
headers={'user-agent': 'foo'}
7474
)
75+
76+
assert len(session.codecs) == 3
7577
assert len(session.transports) == 1
78+
7679
assert session.transports[0].credentials == {'example.org': 'abc'}
7780
assert session.transports[0].headers == {'user-agent': 'foo'}

tests/test_transitions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ def test_delete(doc):
6363
new = session.action(doc, ['nested', 'delete'])
6464
assert new == {}
6565
assert new.title == 'original'
66+
67+
68+
# Test overrides
69+
70+
def test_override_action(doc):
71+
new = session.action(doc, ['nested', 'follow'], action='put')
72+
assert new == {'nested': {'new': 123, 'foo': None}}
73+
assert new.title == 'original'
74+
75+
76+
def test_override_inplace(doc):
77+
new = session.action(doc, ['nested', 'update'], params={'foo': 456}, inplace=False)
78+
assert new == {'new': 123, 'foo': 456}
79+
assert new.title == 'new'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy