This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
fix txn token,dc and consistency #225
Open
amuhametov
wants to merge
17
commits into
python-consul:master
Choose a base branch
from
amuhametov:txn_fix_148
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7350c6a
fix txn token,dc and consistency
ced8fcf
add support to auto base64-decode values
0a6e74e
add decode txn result value option
e2da015
reduce str len
2e4d1bc
add decode txn test
fded800
try to fix test
d2c65a5
str to byte
cf2804c
one more try to make the test successful
f13b8d3
try to fix indent
7019841
try to fix indent
ee901c0
try to fix indent
462901a
try to fix indent
9c56b2f
try to fix indent
5eb01da
Thanks, @matusvalo
75bb0ac
update due to api change
cd0a2c9
Revert "update due to api change"
6fe3a4f
revert auto-decode result, keep the API safe
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,7 +203,8 @@ def json( | |
| one=False, | ||
| decode=False, | ||
| is_id=False, | ||
| index=False): | ||
| index=False, | ||
| txn=False): | ||
| """ | ||
| *map* is a function to apply to the final result. | ||
|
|
||
|
|
@@ -218,6 +219,9 @@ def json( | |
| *decode* if specified this key will be base64 decoded. | ||
|
|
||
| *is_id* only the 'ID' field of the json object will be returned. | ||
|
|
||
| *txn* if specified along with *decode*, the answer will be treated as | ||
| transaction result and all values will be base64-decoded | ||
| """ | ||
| def cb(response): | ||
| CB._status(response, allow_404=allow_404) | ||
|
|
@@ -227,9 +231,16 @@ def cb(response): | |
| data = json.loads(response.body) | ||
|
|
||
| if decode: | ||
| for item in data: | ||
| if item.get(decode) is not None: | ||
| item[decode] = base64.b64decode(item[decode]) | ||
| if txn and data.get('Results') is not None: | ||
| for item in data.get('Results'): | ||
| if item.get('KV').get('Value') is not None: | ||
| item['KV']['Value'] = base64.b64decode( | ||
| item['KV']['Value']) | ||
| elif not txn: | ||
| for item in data: | ||
| if item.get(decode) is not None: | ||
| item[decode] = base64.b64decode(item[decode]) | ||
|
|
||
| if is_id: | ||
| data = data['ID'] | ||
| if one: | ||
|
|
@@ -665,7 +676,13 @@ class Txn(object): | |
| def __init__(self, agent): | ||
| self.agent = agent | ||
|
|
||
| def put(self, payload): | ||
| def put( | ||
| self, | ||
| payload, | ||
| token=None, | ||
| consistency=None, | ||
| dc=None, | ||
| decode=False): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this but I prefer to hide encoding/decoding logic from user. This will break API but it will be more clear. Is there any objection against it? Moreover, new parameters should be documented in doc string.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| """ | ||
| Create a transaction by submitting a list of operations to apply to | ||
| the KV store inside of a transaction. If any operation fails, the | ||
|
|
@@ -686,9 +703,33 @@ def put(self, payload): | |
| "Session": "<session id>" | ||
| } | ||
| } | ||
|
|
||
| *token* is an optional `ACL token`_ to apply to this request. If | ||
| the token's policy is not allowed to delete to this key an | ||
| *ACLPermissionDenied* exception will be raised. | ||
|
|
||
| *consistency* can be either 'default', 'consistent' or 'stale'. if | ||
| not specified *consistency* will the consistency level this client | ||
| was configured with. | ||
|
|
||
| *consistency* the result will be base64-decoded if set to True | ||
| """ | ||
| return self.agent.http.put(CB.json(), "/v1/txn", | ||
| data=json.dumps(payload)) | ||
|
|
||
| params = [] | ||
| token = token or self.agent.token | ||
| if token: | ||
| params.append(('token', token)) | ||
| dc = dc or self.agent.dc | ||
| if dc: | ||
| params.append(('dc', dc)) | ||
| consistency = consistency or self.agent.consistency | ||
| if consistency in ('consistent', 'stale'): | ||
| params.append(('consistency', 1)) | ||
|
|
||
| return self.agent.http.put(CB.json(txn=True, decode=decode), | ||
| "/v1/txn", | ||
| data=json.dumps(payload), | ||
| params=params) | ||
|
|
||
| class Agent(object): | ||
| """ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add new parameters to docs string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed