-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from opendxl/client_credentials_support_impleme…
…ntation Client credentials support implementation
- Loading branch information
Showing
5 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.1" | ||
__version__ = "0.1.2" |
This file contains 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
This file contains 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
58 changes: 58 additions & 0 deletions
58
sample/basic/basic_consume_with_client_credentials_example.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import absolute_import | ||
import json | ||
import os | ||
import sys | ||
from dxlstreamingclient.channel import Channel, ChannelAuth, ClientCredentialsChannelAuth | ||
root_dir = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(root_dir + "/../..") | ||
sys.path.append(root_dir + "/..") | ||
# Import common logging and configuration | ||
from common import * | ||
# Configure local logger | ||
logging.getLogger().setLevel(logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
# Change these below to match the appropriate details for your | ||
# channel connection. | ||
CHANNEL_URL = "http://127.0.0.1:50080" | ||
CHANNEL_IAM_URL = "https://iam.server-cloud.com/" | ||
CHANNEL_CLIENT_ID = "me" | ||
CHANNEL_CLIENT_SECRET = "secret" | ||
CHANNEL_CONSUMER_GROUP = "sample_consumer_group" | ||
CHANNEL_TOPIC_SUBSCRIPTIONS = ["testTopic"] | ||
CHANNEL_SCOPE="" | ||
CHANNEL_GRANT_TYPE="" | ||
CHANNEL_AUDIENCE="" | ||
# Path to a CA bundle file containing certificates of trusted CAs. The CA | ||
# bundle is used to validate that the certificate of the server being connected | ||
# to was signed by a valid authority. If set to an empty string, the server | ||
# certificate is not validated. | ||
VERIFY_CERTIFICATE_BUNDLE = "" | ||
# This constant controls the frequency (in seconds) at which the channel 'run' | ||
# call below polls the streaming service for new records. | ||
WAIT_BETWEEN_QUERIES = 5 | ||
# Create a new channel object | ||
with Channel(CHANNEL_URL, | ||
auth=ClientCredentialsChannelAuth(CHANNEL_IAM_URL, | ||
CHANNEL_CLIENT_ID, | ||
CHANNEL_CLIENT_SECRET, | ||
verify_cert_bundle=VERIFY_CERTIFICATE_BUNDLE, | ||
scope=CHANNEL_SCOPE, | ||
grant_type=CHANNEL_GRANT_TYPE, | ||
audience=CHANNEL_AUDIENCE), | ||
consumer_group=CHANNEL_CONSUMER_GROUP, | ||
verify_cert_bundle=VERIFY_CERTIFICATE_BUNDLE | ||
) as channel: | ||
# Create a function which will be called back upon by the 'run' method (see | ||
# below) when records are received from the channel. | ||
def process_callback(payloads): | ||
# Print the payloads which were received. 'payloads' is a list of | ||
# dictionary objects extracted from the records received from the | ||
# channel. | ||
logger.info("Received payloads: \n%s", | ||
json.dumps(payloads, indent=4, sort_keys=True)) | ||
# Return 'True' in order for the 'run' call to continue attempting to | ||
# consume records. | ||
return True | ||
# Consume records indefinitely | ||
channel.run(process_callback, wait_between_queries=WAIT_BETWEEN_QUERIES, | ||
topics=CHANNEL_TOPIC_SUBSCRIPTIONS) |
65 changes: 65 additions & 0 deletions
65
sample/basic/basic_produce_with_client_credentials_example.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import absolute_import | ||
from __future__ import print_function | ||
import base64 | ||
import json | ||
import os | ||
import sys | ||
from dxlstreamingclient.channel import Channel, ChannelAuth , ClientCredentialsChannelAuth | ||
root_dir = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(root_dir + "/../..") | ||
sys.path.append(root_dir + "/..") | ||
# Import common logging and configuration | ||
from common import * | ||
# Configure local logger | ||
logging.getLogger().setLevel(logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
# Change these below to match the appropriate details for your | ||
# channel connection. | ||
CHANNEL_URL = "http://127.0.0.1:50080" | ||
CHANNEL_IAM_URL = "https://iam.server-cloud.com/" | ||
CHANNEL_CLIENT_ID = "me" | ||
CHANNEL_CLIENT_SECRET = "secret" | ||
CHANNEL_TOPIC = ["testTopic"] | ||
CHANNEL_SCOPE="" | ||
CHANNEL_GRANT_TYPE="" | ||
CHANNEL_AUDIENCE="" | ||
# Path to a CA bundle file containing certificates of trusted CAs. The CA | ||
# bundle is used to validate that the certificate of the server being connected | ||
# to was signed by a valid authority. If set to an empty string, the server | ||
# certificate is not validated. | ||
VERIFY_CERTIFICATE_BUNDLE = "" | ||
# Create the message payload to be included in a record | ||
message_payload = { | ||
"message": "Hello from OpenDXL" | ||
} | ||
# Create the full payload with records to produce to the channel | ||
channel_payload = { | ||
"records": [ | ||
{ | ||
"routingData": { | ||
"topic": CHANNEL_TOPIC, | ||
"shardingKey": "" | ||
}, | ||
"message": { | ||
"headers": {}, | ||
# Convert the message payload from a dictionary to a | ||
# base64-encoded string. | ||
"payload": base64.b64encode( | ||
json.dumps(message_payload).encode()).decode() | ||
} | ||
} | ||
] | ||
} | ||
# Create a new channel object | ||
with Channel(CHANNEL_URL, | ||
auth=ClientCredentialsChannelAuth(CHANNEL_IAM_URL, | ||
CHANNEL_CLIENT_ID, | ||
CHANNEL_CLIENT_SECRET, | ||
verify_cert_bundle=VERIFY_CERTIFICATE_BUNDLE, | ||
scope=CHANNEL_SCOPE, | ||
grant_type=CHANNEL_GRANT_TYPE, | ||
audience=CHANNEL_AUDIENCE), | ||
verify_cert_bundle=VERIFY_CERTIFICATE_BUNDLE) as channel: | ||
# Produce the payload records to the channel | ||
channel.produce(channel_payload) | ||
print("Succeeded.") |