Skip to content

Commit

Permalink
Merge pull request #5 from electricimp/develop
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
Pavel Petroshenko authored Aug 21, 2018
2 parents 2e0210b + 732843a commit b0c4176
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
11 changes: 8 additions & 3 deletions AWSKinesisStreams.agent.lib.nut
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const _AWS_KINESIS_STREAMS_SERVICE = "kinesis";
const _AWS_KINESIS_STREAMS_TARGET_PREFIX = "Kinesis_20131202";

class AWSKinesisStreams {
static VERSION = "1.0.0";
static VERSION = "1.1.0";

// Enables/disables the library debug output (including errors logging).
// Disabled by default.
Expand Down Expand Up @@ -717,6 +717,7 @@ class AWSKinesisStreams.Record {
_error = null;
_explicitHashKey = null;
_prevSequenceNumber = null;
_encoder = null;

// Creates and returns AWSKinesisStreams.Record object that can be written into an
// Amazon Kinesis stream using AWSKinesisStreams.Producer methods.
Expand All @@ -733,15 +734,19 @@ class AWSKinesisStreams.Record {
// prevSequenceNumber : See http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-SequenceNumberForOrdering
// string (optional)
//
// encoder : a custom JSON encoder function for encoding the provided data (e.g. [JSONEncoder.encode](https://github.com/electricimp/JSONEncoder))
// function (otpional)
//
// Returns: AWSKinesisStreams.Record object that can be
// written into the Amazon Kinesis stream using
// AWSKinesisStreams.Producer putRecord/putRecords
// methods.
constructor(data, partitionKey, explicitHashKey = null, prevSequenceNumber = null) {
constructor(data, partitionKey, explicitHashKey = null, prevSequenceNumber = null, encoder = null) {
this.data = data;
this.partitionKey = partitionKey;
_explicitHashKey = explicitHashKey;
_prevSequenceNumber = prevSequenceNumber;
_encoder = encoder == null ? http.jsonencode.bindenv(http) : encoder;
}

// -------------------- PRIVATE METHODS -------------------- //
Expand All @@ -750,7 +755,7 @@ class AWSKinesisStreams.Record {
_error = AWSKinesisStreams._utils._validateNonEmpty(partitionKey, "partitionKey");
if (!_error) {
local result = {
"Data" : http.base64encode(typeof data == "blob" ? data.tostring() : http.jsonencode(data)),
"Data" : http.base64encode(typeof data == "blob" ? data.tostring() : _encoder(data)),
"PartitionKey" : partitionKey,
};
if (_explicitHashKey) {
Expand Down
2 changes: 1 addition & 1 deletion Examples/DataConsumer.agent.nut
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"

// AWSKinesisStreams.Consumer library sample.
// Periodically reads new data records from all shards of the specified preconfigured
Expand Down
2 changes: 1 addition & 1 deletion Examples/DataProducer.agent.nut
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"

// AWSKinesisStreams.Producer library sample.
// Periodically writes data records to the specified preconfigured AWS Kinesis Stream
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The AWSKinesisStreams library utilizes the [AWSRequestV4](https://github.com/ele

```squirrel
#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
```

## Library Usage ##
Expand Down Expand Up @@ -68,7 +68,7 @@ This class represents an AWS Kinesis Streams record: a combination of data attri
| *timestamp* | Integer | The approximate time that the record was inserted into the stream. In number of seconds since Unix epoch (midnight, 1 Jan 1970) |
| *encryptionType* | [AWS_KINESIS_STREAMS_ENCRYPTION_TYPE](#aws_kinesis_streams_encryption_type-enum) | The encryption type used on the record |

### Constructor: AWSKinesisStreams.Record(*data, partitionKey[, explicitHashKey][, prevSequenceNumber]*) ###
### Constructor: AWSKinesisStreams.Record(*data, partitionKey[, explicitHashKey][, prevSequenceNumber][, encoder]*) ###

This method creates and returns an AWSKinesisStreams.Record object that can be written into an Amazon Kinesis stream using [AWSKinesisStreams.Producer](#awskinesisstreamsproducer-class) methods.

Expand All @@ -78,6 +78,7 @@ This method creates and returns an AWSKinesisStreams.Record object that can be w
| *partitionKey* | String | Yes | Identifies which shard in the stream the data record is assigned to (see the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_Record.html#Streams-Type-Record-PartitionKey)) |
| *explicitHashKey* | String | Optional | The hash value used to explicitly determine the shard the data record is assigned to by overriding the partition key hash (see the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-ExplicitHashKey)) |
| *prevSequenceNumber* | String | Optional | See the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-SequenceNumberForOrdering) |
| *encoder* | Function | Optional | A custom JSON encoder function for encoding the provided data (e.g. [JSONEncoder.encode](https://github.com/electricimp/JSONEncoder)) |

## AWS_KINESIS_STREAMS_ENCRYPTION_TYPE Enum ##

Expand Down Expand Up @@ -268,7 +269,26 @@ A type of Squirrel data which can be encoded/decoded into/from JSON, eg. table,

```squirrel
#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
#require "JSONEncoder.class.nut:2.0.0"
//This class can be used to hold numbers larger than Squirrel can natively support (i.e. anything larger than 32-bit)
//and then be encoded as a number (rather than a string) when encoded with `JSONEncoder.encode`.
class JSONLiteralString {
_string = null;
constructor (string) {
_string = string.tostring();
}
function _serializeRaw() {
return _string;
}
function toString() {
return _string;
}
}
// Substitute with real values
const AWS_KINESIS_REGION = "<YOUR_AWS_REGION>";
Expand All @@ -292,7 +312,8 @@ producer.putRecord(AWSKinesisStreams.Record("Hello!", "partitionKey"), function
records <- [
AWSKinesisStreams.Record("test", "partitionKey1"),
AWSKinesisStreams.Record(12345, "partitionKey2"),
AWSKinesisStreams.Record({ "temperature" : 21, "humidity" : 60 }, "partitionKey3")
AWSKinesisStreams.Record({ "temperature" : 21, "humidity" : 60 }, "partitionKey3"),
AWSKinesisStreams.Record({ "a" : JSONLiteralString("123456789123456789") }, "partitionKey4", null, null, JSONEncoder.encode.bindenv(JSONEncoder)) //write record using custom encoder
];
producer.putRecords(records, function (error, failedRecordCount, putResults) {
Expand All @@ -315,7 +336,7 @@ producer.putRecords(records, function (error, failedRecordCount, putResults) {

```squirrel
#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
// Substitute with real values
const AWS_KINESIS_REGION = "<YOUR_AWS_REGION>";
Expand Down

0 comments on commit b0c4176

Please sign in to comment.