Skip to content

Commit

Permalink
Fix b64 data field name.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Sep 29, 2020
1 parent b4bea61 commit 7464342
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ Unofficial Python implementation for [CloudEvents](https://cloudevents.io/) v1.0
Check out the [CloudEvents spec](https://github.com/cloudevents/spec/blob/v1.0/spec.md).

This package has no dependencies beyond the Python standard library with the base install.
Optionally depends on the `avro` package for Avro encode/decode functionality.
Optionally depends on the `avro` package for Avro encode/decode functionality.

## News

### 0.2.2 - (*2020-09-29*)

* First public release.

## Install

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='spce',
version='0.2.1',
version='0.2.2',
packages=['spce'],
url='https://github.com/scaleplandev/spce-python',
license='Apache 2.0',
Expand Down
8 changes: 4 additions & 4 deletions spce/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def encode(cls, event: CloudEvent):
kvs.append('"%s":%s' % (attr, encoder.encode(value)))
if event._data:
if event._has_binary_data:
kvs.append('"data_b64":%s' % encoder.encode(b64encode(event._data).decode()))
kvs.append('"data_base64":%s' % encoder.encode(b64encode(event._data).decode()))
else:
kvs.append('"data":%s' % encoder.encode(event._data))
return "{%s}" % ",".join(kvs)

@classmethod
def decode(cls, text: str) -> CloudEvent:
d = json.loads(text)
if "data_b64" in d:
d["data"] = b64decode(d["data_b64"])
del d["data_b64"]
if "data_base64" in d:
d["data"] = b64decode(d["data_base64"])
del d["data_base64"]

return CloudEvent(**d)
4 changes: 2 additions & 2 deletions tests/json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_encode_binary_data(self):
"id": "1000",
"specversion": "1.0",
"datacontenttype": "application/octet-stream",
"data_b64": "AQIDBA=="
"data_base64": "AQIDBA=="
}
'''
self.assertEqual(json.loads(target), json.loads(encoded))
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_decode_binary_data(self):
"id": "1000",
"specversion": "1.0",
"datacontenttype": "application/octet-stream",
"data_b64": "AQIDBA=="
"data_base64": "AQIDBA=="
}
'''
target = CloudEvent(
Expand Down

0 comments on commit 7464342

Please sign in to comment.