Skip to content

Commit 4b17949

Browse files
committed
Implement read logic.
1 parent 11f755e commit 4b17949

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

.vscode/launch.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/bin/Debug/net6.0/carol-poc.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false
17+
},
18+
{
19+
"name": ".NET Core Attach",
20+
"type": "coreclr",
21+
"request": "attach"
22+
}
23+
]
24+
}

.vscode/tasks.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/carol-poc.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/carol-poc.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/carol-poc.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Program.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// The built-in .Net HttpClient could be used
2+
// But flurl has a nicer API and deserializes the JSON for us
3+
using Flurl.Http;
4+
5+
var host = "{provide host}";
6+
var username = "{provide username}";
7+
8+
// For test systems only. Never embed plain text passwords in your code.
9+
var password = "{provide password}";
10+
11+
// Object IDs are available within MUI or using GET /objectIdentifiers?fqr={fqr}
12+
var object1Id = "960534d2-7df5-5ed8-884c-164e7a2f280a";
13+
var object2Id = "c82d160b-884c-5eac-bad4-89754f0069e0";
14+
15+
// Login and get a token
16+
string accessToken;
17+
18+
using (var client = new FlurlClient($"https://{host}/api/v3"))
19+
{
20+
var loginObject = new
21+
{
22+
Username = username,
23+
Password = password
24+
};
25+
26+
// This line invokes the operation: POST /api/v3/login
27+
accessToken = (await client.Request($"https://{host}/api/v3/login").PostJsonAsync(loginObject)
28+
.ReceiveJson()).accessToken;
29+
}
30+
31+
// Use the token to read two attributes
32+
33+
// Creates an http client with base url of our API and using the access token
34+
using (var client = new FlurlClient($"https://{host}/api/v3").WithOAuthBearerToken(accessToken))
35+
{
36+
// The next two lines actually read the present value of 2 objects.
37+
// They are invoking the operation: GET /objects/{id}/attributes/presentValue
38+
var attribute1Response = await client.Request($"https://{host}/api/v3/objects/{object1Id}/attributes/presentValue").GetJsonAsync();
39+
var attribute2Response = await client.Request($"https://{host}/api/v3/objects/{object2Id}/attributes/presentValue").GetJsonAsync();
40+
41+
// The next two lines take advantage of the fact that attribute1Response
42+
// and attribute2Response are dynamic objects that allow us to query the
43+
// responses with properties.
44+
Console.WriteLine($"Value 1: {attribute1Response.item.presentValue}");
45+
Console.WriteLine($"Value 2: {attribute2Response.item.presentValue}");
46+
}

carol-poc.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>carol_poc</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Flurl.Http" Version="3.2.2" />
13+
<PackageReference Include="NewtonSoft.Json" Version="13.0.1" />
14+
</ItemGroup>
15+
16+
</Project>

0 commit comments

Comments
 (0)