Git Product home page Git Product logo

cloudevents-sdk-cpp's Introduction

Build Status

C++ SDK for CloudEvents

This SDK is still a work in progress.

The CloudEvent class (generated from protobuf) is based on this open PR to the CloudEvent spec from JemDay.

The PubsubMessage class (generated from protobuf) is based on Google API.

This is not an officially supported Google product.

User Guide

More example use cases are available in header files as comments. The only files that you will directly import and use are in //v1/protocol_binding/.

Setup

Install Bazel Build System

Instuctions found in Bazel documentation.

Import SDK as a Bazel package in WORKSPACE

In //WORKSPACE

# TODO (#63) : Update README to create Bazel target to master repo once merged

git_repository(
    name = "cloudevents_cpp_sdk",
    commit = "20c9855c11bc66efcc6e086571477bb69870be1d",
    remote = "https://github.com/michlee1337/cloudevents-sdk-cpp/tree/binder-all.v2",
)

Set up the build dependencies

Specify any binders needed as a bazel target dependency.

To use PubsubBinder:

cc_binary(
    ...
    deps = [
        ...
        "@cloudevents_cpp_sdk/protocol_binding:pubsub_binder_lib",
    ],
)

To use HttpReqBinder or HttpResBinder:

cc_binary(
    ...
    deps = [
        ...
        "@cloudevents_cpp_sdk/protocol_binding:http_binder_lib",
    ],
)

Binding a CloudEvent to a Protocol-Message in Binary-ContentMode

Import the header file and use XBinder

// import the header for the binder
#include "//v1/protocol_binding/pubsub_binder.h"

using ::google::pubsub::v1::PubsubMessage;
using ::cloudevents::binder::PubsubBinder;
using ::cloudevents_absl::StatusOr;

// create a CloudEvent
CloudEvent my_cloud_event;
my_cloud_event.set_id("my_id");
my_cloud_event.set_source("my_source");
my_cloud_event.set_spec_version("1.0");
my_cloud_event.set_type("my_type");
my_cloud_event.set_binary_data("1010");

// Initialize the binder
PubsubBinder pubsub_binder;

// Create the Message
StatusOr<PubsubMessage> bind = pubsub_binder.Bind(my_cloud_event);

// Check no errors
if (!bind.ok()) {
  std::cerr << bind.status();
}

// unwrap message
PubsubMessage usable_message = *bind;

Binding a CloudEvent to a Protocol-Message in Structured-ContentMode

Import the header file and use Format and XBinder

// import the header for the binder
#include "//v1/protocol_binding/pubsub_binder.h"

using ::google::pubsub::v1::PubsubMessage;
using ::cloudevents::binder::PubsubBinder
using ::cloudevents::format::Format
using ::cloudevents_absl::StatusOr;

// create a CloudEvent
CloudEvent my_cloud_event;
my_cloud_event.set_id("my_id");
my_cloud_event.set_source("my_source");
my_cloud_event.set_spec_version("1.0");
my_cloud_event.set_type("my_type");
my_cloud_event.set_binary_data("1010");

// initialize the binder
PubsubBinder pubsub_binder;

// specify the EventFormat to be used and create the Message
StatusOr<PubsubMessage> bind = pubsub_binder.Bind(my_cloud_event, Format::kJson);

// check no errors
if (!bind.ok()) {
  std::cerr << bind.status();
}

// unwrap message
PubsubMessage usable_message = *bind;

Unbind a CloudEvent from a Protocol-Message

// import the header for the binder of the protocol message you're working with
#include "//v1/protocol_binding/pubsub_binder.h"

using ::google::pubsub::v1::PubsubMessage;
using ::cloudevents::binder::Binder
using ::cloudevents_absl::StatusOr;

PubsubMessage my_pusub_msg = ...; // However you get this message

// initialize the binder
PubsubBinder pubsub_binder;

// create the CloudEvent
StatusOr<CloudEvent> unbind = pubsub_binder.Unbind(my_pusub_msg);

// check no errors
if (!unbind.ok()) {
  std::cerr << unbind.status();
}

// unwrap cloudevent
CloudEvent cloud_event = *unbind;

Samples

Run-able code samples are available in the /samples folder.

Run

  1. Create executable
    bazel build //sample:create_event
  2. Run executable
    bazel-bin/sample/create_event

File Structure

All logic related to implementing version 1 of the CloudEvent spec can be found in //v1.

  • All logic for Protocol Bindings can be found in the subfolder //v1/protocol_binding.
  • All logic for Event Formats can be found in the subfolder //v1/event_format.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.