Skip to content

Commit 74e2cc2

Browse files
author
Michael Gasch
authored
Merge pull request #21 from dmilov/topic/dmilov/update-readme-examples
2 parents 427100c + 3a9010a commit 74e2cc2

File tree

1 file changed

+81
-131
lines changed

1 file changed

+81
-131
lines changed

README.md

Lines changed: 81 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Supported CloudEvents versions:
88
Supported Protocols:
99
- HTTP
1010

11-
# **CloudEvents.Sdk** Module
11+
## **`CloudEvents.Sdk`** Module
1212
The module contains functions to
1313
- Create CloudEvent objects
1414
- Add data to a CloudEvent object
1515
- Read data from a CloudEvent object
1616
- Convert a CloudEvent object to an HTTP Message
1717
- Convert an HTTP Message to a CloudEvent object
1818

19-
## Install **CloudEvents.Sdk** Module
19+
## Install **`CloudEvents.Sdk`** Module
2020

2121
### Prerequisites
2222
- [PowerShell 7.0](https://github.com/PowerShell/PowerShell/releases/tag/v7.0.4)
@@ -38,174 +38,124 @@ Function Set-CloudEventJsonData 0.2.0 Cl
3838
Function Set-CloudEventXmlData 0.2.0 CloudEvents.Sdk
3939
```
4040

41-
## Event Producer
41+
## Using **`CloudEvents.Sdk`** Module
42+
## 1. Event Producer
4243
### Create a CloudEvent object
4344
```powershell
44-
$cloudEvent = New-CloudEvent -Type 'com.example.object.deleted.v2' -Source 'mailto:[email protected]' -Id '6e8bc430-9c3a-11d9-9669-0800200c9a66' -Time (Get-Date)
45+
$cloudEvent = New-CloudEvent -Type 'com.example.object.deleted.v2' -Source 'mailto:[email protected]' -Id (New-Guid).Guid -Time (Get-Date)
4546
```
4647

47-
### Add **JSON Data** to a CloudEvent object
48+
### Set **JSON Data** to a CloudEvent object
4849
```powershell
49-
$cloudEvent | Add-CloudEventJsonData -Data @{
50-
'key1' = 'value1'
51-
'key2' = @{
52-
'key3' = 'value3'
53-
}
50+
$cloudEvent | Set-CloudEventJsonData -Data @{
51+
'Foo' = 'Hello'
52+
'Bar' = 'World'
5453
}
54+
55+
56+
DataContentType : application/json
57+
Data : {
58+
"Bar": "World",
59+
"Foo": "Hello"
60+
}
61+
Id : ac9b12d9-ae45-4654-a4d7-42bbf0d9816d
62+
DataSchema :
63+
Source : mailto:[email protected]
64+
SpecVersion : V1_0
65+
Subject :
66+
Time : 4/26/2021 9:00:45 AM
67+
Type : com.example.object.deleted.v2
5568
```
5669

57-
### Add **XML Data** to a CloudEvent object
70+
### Set **XML Data** to a CloudEvent object
5871
```powershell
59-
$cloudEvent | Add-CloudEventXmlData -Data @{
60-
'key1' = @{
61-
'key2' = 'value'
62-
}
72+
$cloudEvent | Set-CloudEventXmlData -Data @{
73+
'xml' = @{
74+
'Foo' = 'Hello'
75+
'Bar' = 'World'
76+
}
6377
} `
6478
-AttributesKeysInElementAttributes $true
79+
80+
81+
DataContentType : application/xml
82+
Data : <xml><Bar>World</Bar><Foo>Hello</Foo></xml>
83+
Id : ac9b12d9-ae45-4654-a4d7-42bbf0d9816d
84+
DataSchema :
85+
Source : mailto:[email protected]
86+
SpecVersion : V1_0
87+
Subject :
88+
Time : 4/26/2021 9:00:45 AM
89+
Type : com.example.object.deleted.v2
6590
```
66-
`AttributesKeysInElementAttributes` specifies how to format the XML. If `true` and the input Data hashtable has pairs of 'Attributes', 'Value' keys creates XML element with attributes, otherwise each key is formatted as XML element.<br/>
67-
If `true`
68-
```powershell
69-
@{'root' = @{'Attributes' = @{'att1' = 'true'}; 'Value' = 'val-1'}}
70-
```
71-
is formatted as
72-
```xml
73-
<root att1="true">val-1</root>
74-
```
75-
If `false`
91+
### Set Custom Format Data to a CloudEvent object
7692
```powershell
77-
@{'root' = @{'Attributes' = @{'att1' = 'true'}; 'Value' = 'val-1'}}
78-
```
79-
is formatted as
80-
```xml
81-
<root><Attributes><att1>true</att1></Attributes><Value>val-1</Value></root>
82-
```
93+
$cloudEvent | Set-CloudEventData -DataContentType 'application/text' -Data 'Hello World!'
8394
84-
#### Add Custom Format Data to a CloudEvent object
85-
```powershell
86-
$cloudEvent | Add-CloudEventData -DataContentType 'application/text' -Data 'wow'
95+
DataContentType : application/text
96+
Data : Hello World!
97+
Id : b1b748cd-e98d-4f5f-80ea-76dea71a53a5
98+
DataSchema :
99+
Source : mailto:[email protected]
100+
SpecVersion : V1_0
101+
Subject :
102+
Time : 4/27/2021 7:00:44 PM
103+
Type : com.example.object.deleted.v2
87104
```
88105

89106
### Convert a CloudEvent object to an HTTP message in **Binary** or **Structured** content mode
90107
```powershell
91-
$cloudEventBinaryHttpMessage = $cloudEvent | ConvertTo-HttpMessage -ContentMode Binary
108+
# Format structured cloud event HTTP message
92109
$cloudEventStructuredHttpMessage = $cloudEvent | ConvertTo-HttpMessage -ContentMode Structured
93110
```
94111

95112
### Send CloudEvent object to HTTP server
96113
```powershell
97-
Invoke-WebRequest -Method POST -Uri 'http://my.cloudevents.server/' -Headers $cloudEventBinaryHttpMessage.Headers -Body $cloudEventBinaryHttpMessage.Body
114+
Invoke-WebRequest -Method POST -Uri 'http://my.cloudevents.server/' -Headers $cloudEventStructuredHttpMessage.Headers -Body $cloudEventStructuredHttpMessage.Body
98115
```
99116

100-
## Event Consumer
117+
## 2. Event Consumer
101118
### Convert an HTTP message to a CloudEvent object
102119
```powershell
103120
$cloudEvent = ConvertFrom-HttpMessage -Headers <headers> -Body <body>
104121
```
105122

106123
### Read CloudEvent **JSON Data** as a **PowerShell Hashtable**
107124
```powershell
108-
$hashtableData = Read-CloudEventJsonData -CloudEvent $cloudEvent
125+
Read-CloudEventJsonData -CloudEvent $cloudEvent
126+
127+
128+
Name Value
129+
---- -----
130+
Foo Hello
131+
Bar World
109132
```
110133

111134
### Read CloudEvent **XML Data** as a **PowerShell Hashtable**
112135
```powershell
113-
$hashtableData = Read-CloudEventXmlData -CloudEvent $cloudEvent -ConvertMode SkipAttributes
114-
```
115-
The `ConvertMode` parameter specifies how the XML to be represented in the result hashtable<br/>
116-
`SkipAttributes` - Skips attributes of the XML elements. XmlElement is a Key-Value pair where Key is the Xml element name, and the value is the Xml element inner text<br/>
117-
Example:
118-
```xml
119-
<key att='true'>value1</key>
120-
```
121-
is converted to
122-
```powershell
123-
@{'key' = 'value-1'}
124-
```
125-
`AlwaysAttrValue` - Each element is a HashTable with two keys<br/>
126-
'Attributes' - key-value pair of the Xml element attributes if any, otherwise null<br/>
127-
'Value' - string value represinting the xml element inner text<br/>
128-
Example:
129-
```xml
130-
```
131-
<key1 att='true'>value1</key1><key2>value2</key2>
132-
is converted to
133-
```powershell
134-
@{
135-
'key1' = @{
136-
'Attributes' = @{
137-
'att' = 'true'
138-
}
139-
'Value' = 'value1'
140-
}
141-
'key2' = @{
142-
'Attributes' = $null
143-
'Value' = 'value2'
144-
}
145-
}
146-
```
147-
`AttrValueWhenAttributes` - Uses `SkipAttributes` for xml elements without attributes and `AlwaysAttrValue` for xml elements with attributes<br/>
148-
Example:
149-
```xml
150-
<key1 att='true'>value1</key1><key2>value2</key2>
151-
```
152-
is converted to
153-
```powershell
154-
@{
155-
'key1' = @{
156-
'Attributes' = @{
157-
'att' = 'true'
158-
}
159-
'Value' = 'value1'
160-
}
161-
'key2' = 'value2'
162-
}
163-
```
136+
Read-CloudEventXmlData -CloudEvent $cloudEvent -ConvertMode SkipAttributes
164137
165-
### Read CloudEvent Custom Format **Data** as a **byte[]**
166-
```powershell
167-
$bytes = Read-CloudEventData -CloudEvent $cloudEvent
138+
Name Value
139+
---- -----
140+
xml {Bar, Foo}
168141
```
169142

170-
## Build the **CloudEvents.Sdk** Module
171-
172-
The `build.ps1` script
173-
- Creates the CloudEvents PowerShell Module in a `CloudEvents` directory.
174-
- Runs functions unit tests
175-
- Runs local integrations tests
176-
- Creates a catalog file for the CloudEvents Module
177-
178-
### Prerequisites
179-
- [PowerShell 7.0](https://github.com/PowerShell/PowerShell/releases/tag/v7.0.4)
180-
- [Pester 5.1.1](https://www.powershellgallery.com/packages/Pester/5.1.1)
181-
- [dotnet SDK](https://dotnet.microsoft.com/download/dotnet/5.0)
143+
The `ConvertMode` parameter specifies how the xml should be converted to a PowerShell Hashtable. `SkipAttributes` mode skips reading the xml attributes. There are three different modes of conversion. For more details check the help of the `Read-CloudEventXmlData` cmdlet.
182144

145+
### Read CloudEvent Custom Format **Data** as a **byte[]**
183146
```powershell
184-
> ./build.ps1
185-
[9:52:42 AM] INFO: Publish CloudEvents.Sdk Module to 'C:\git-repos\cloudevents\cloudevents-sdk-powershell\CloudEvents.Sdk'
186-
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
187-
Copyright (C) Microsoft Corporation. All rights reserved.
188-
189-
Determining projects to restore...
190-
All projects are up-to-date for restore.
191-
CloudEventsPowerShell -> C:\git-repos\cloudevents\cloudevents-sdk-powershell\src\CloudEventsPowerShell\bin\Release\netstandard2.0\CloudEventsPowerShell.dll
192-
CloudEventsPowerShell -> C:\git-repos\cloudevents\cloudevents-sdk-powershell\CloudEvents.Sdk\
193-
[9:52:44 AM] INFO: Run unit tests
194-
195-
Starting discovery in 9 files.
196-
Discovery finished in 294ms.
197-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Add-CloudEventData.Tests.ps1 1.01s (184ms|656ms)
198-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Add-CloudEventJsonData.Tests.ps1 329ms (39ms|279ms) [+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Add-CloudEventXmlData.Tests.ps1 336ms (58ms|267ms) [+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\ConvertFrom-HttpMessage.Tests.ps1 557ms (203ms|337ms) [+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\ConvertTo-HttpMessage.Tests.ps1 508ms (132ms|361ms) [+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\New-CloudEvent.Tests.ps1 275ms (22ms|243ms)
199-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Read-CloudEventData.Tests.ps1 257ms (10ms|236ms)
200-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Read-CloudEventJsonData.Tests.ps1 308ms (40ms|257ms)
201-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\unit\Read-CloudEventXmlData.Tests.ps1 310ms (53ms|246ms)
202-
Tests completed in 3.94s
203-
Tests Passed: 28, Failed: 0, Skipped: 0 NotRun: 0
204-
[9:52:49 AM] INFO: Run integration tests
205-
206-
Starting discovery in 1 files.
207-
Discovery finished in 176ms.
208-
[+] C:\git-repos\cloudevents\cloudevents-sdk-powershell\test\integration\HttpIntegration.Tests.ps1 2.54s (1.77s|617ms)
209-
Tests completed in 2.56s
210-
Tests Passed: 5, Failed: 0, Skipped: 0 NotRun: 0
147+
Read-CloudEventData -CloudEvent $cloudEvent
148+
149+
72
150+
101
151+
108
152+
108
153+
111
154+
32
155+
87
156+
111
157+
114
158+
108
159+
100
160+
33
211161
```

0 commit comments

Comments
 (0)