|
| 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 | +} |
0 commit comments