Skip to content

Commit b06ed3b

Browse files
authored
Merge pull request #19 from databox/pushdata-example
Added example
2 parents 8d6b1d8 + e1ef703 commit b06ed3b

File tree

6 files changed

+148
-140
lines changed

6 files changed

+148
-140
lines changed

.github/workflows/generate_sdk_code.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ jobs:
7878
run: |
7979
java --version
8080
java -jar ${{ runner.temp }}/openapi-generator-cli.jar generate -i ${{ runner.temp }}/openapispec/openapi.yml -g csharp -o ./src -c ${{ runner.temp }}/${{ env.CONFIG_FILE }} --skip-validate-spec
81-
cp ./src/README.md ./README.md
8281
cp -r ./src/docs ./docs
8382
8483
- name: Create Pull Request

README.md

Lines changed: 50 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,80 @@
1-
# Databox - the C# library for the Static OpenAPI document of Push API resource
1+
## Databox
22

3-
Push API resources Open API documentation
3+
This package is designed to consume the Databox Push API functionality via .NET based client.
44

5-
This C# SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:
5+
Supported .NET versions: .NET 6.0 and later.
66

7-
- API version: 0.4.4-alpha.4
8-
- SDK version: 0.0.3
9-
- Generator version: 7.6.0
10-
- Build package: org.openapitools.codegen.languages.CSharpClientCodegen
11-
12-
<a id="frameworks-supported"></a>
13-
## Frameworks supported
14-
15-
<a id="dependencies"></a>
16-
## Dependencies
17-
18-
- [Json.NET](https://www.nuget.org/packages/Newtonsoft.Json/) - 13.0.2 or later
19-
- [JsonSubTypes](https://www.nuget.org/packages/JsonSubTypes/) - 1.8.0 or later
20-
- [System.ComponentModel.Annotations](https://www.nuget.org/packages/System.ComponentModel.Annotations) - 5.0.0 or later
21-
22-
The DLLs included in the package may not be the latest version. We recommend using [NuGet](https://docs.nuget.org/consume/installing-nuget) to obtain the latest version of the packages:
23-
```
24-
Install-Package Newtonsoft.Json
25-
Install-Package JsonSubTypes
26-
Install-Package System.ComponentModel.Annotations
27-
```
28-
<a id="installation"></a>
297
## Installation
30-
Run the following command to generate the DLL
31-
- [Mac/Linux] `/bin/sh build.sh`
32-
- [Windows] `build.bat`
338

34-
Then include the DLL (under the `bin` folder) in the C# project, and use the namespaces:
35-
```csharp
36-
using Databox.Api;
37-
using Databox.Client;
38-
using Databox.Model;
39-
```
40-
<a id="packaging"></a>
41-
## Packaging
9+
The package is listed as **public** in our Databox Github repository. In order to consume it, you must first add a **nuget source**.
4210

43-
A `.nuspec` is included with the project. You can follow the Nuget quickstart to [create](https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package#create-the-package) and [publish](https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package#publish-the-package) packages.
11+
Databox package repository url is: https://nuget.pkg.github.com/databox/index.json
4412

45-
This `.nuspec` uses placeholders from the `.csproj`, so build the `.csproj` directly:
13+
Detail instructions on how to add this to your project is available [here](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry).
4614

47-
```
48-
nuget pack -Build -OutputDirectory out Databox.csproj
49-
```
50-
51-
Then, publish to a [local feed](https://docs.microsoft.com/en-us/nuget/hosting-packages/local-feeds) or [other host](https://docs.microsoft.com/en-us/nuget/hosting-packages/overview) and consume the new package via Nuget as usual.
15+
After this is completed, the package can be installed via IDE (package named **"Databox"**) or via **dotnet cli** command
5216

53-
<a id="usage"></a>
54-
## Usage
55-
56-
To use the API client with a HTTP proxy, setup a `System.Net.WebProxy`
57-
```csharp
58-
Configuration c = new Configuration();
59-
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
60-
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
61-
c.Proxy = webProxy;
6217
```
63-
64-
### Connections
65-
Each ApiClass (properly the ApiClient inside it) will create an instance of HttpClient. It will use that for the entire lifecycle and dispose it when called the Dispose method.
66-
67-
To better manager the connections it's a common practice to reuse the HttpClient and HttpClientHandler (see [here](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net) for details). To use your own HttpClient instance just pass it to the ApiClass constructor.
68-
69-
```csharp
70-
HttpClientHandler yourHandler = new HttpClientHandler();
71-
HttpClient yourHttpClient = new HttpClient(yourHandler);
72-
var api = new YourApiClass(yourHttpClient, yourHandler);
18+
dotnet add package Databox --version <version>
7319
```
7420

75-
If you want to use an HttpClient and don't have access to the handler, for example in a DI context in Asp.net Core when using IHttpClientFactory.
21+
### Prerequisites
7622

77-
```csharp
78-
HttpClient yourHttpClient = new HttpClient();
79-
var api = new YourApiClass(yourHttpClient);
80-
```
81-
You'll loose some configuration settings, the features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. You need to either manually handle those in your setup of the HttpClient or they won't be available.
82-
83-
Here an example of DI setup in a sample web project:
84-
85-
```csharp
86-
services.AddHttpClient<YourApiClass>(httpClient =>
87-
new PetApi(httpClient));
88-
```
23+
In use the Databox Push API functionality, please refer to [Databox Developers Page](https://developers.databox.com/), specifically the **Quick Guide** section, where you will learn how to create a **Databox Push API token** which is required for pushing your data.
8924

25+
### Example
9026

91-
<a id="getting-started"></a>
92-
## Getting Started
27+
The basic example of pushing data to Databox is provided below:
9328

9429
```csharp
95-
using System.Collections.Generic;
9630
using System.Diagnostics;
97-
using System.Net.Http;
9831
using Databox.Api;
9932
using Databox.Client;
10033
using Databox.Model;
10134

10235
namespace Example
10336
{
104-
public class Example
37+
public class Example
38+
{
39+
public static async Task Main(string[] args)
10540
{
106-
public static void Main()
107-
{
108-
109-
Configuration config = new Configuration();
110-
config.BasePath = "https://push.databox.com";
111-
// Configure HTTP basic authorization: basicAuth
112-
config.Username = "YOUR_USERNAME";
113-
config.Password = "YOUR_PASSWORD";
114-
115-
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
116-
HttpClient httpClient = new HttpClient();
117-
HttpClientHandler httpClientHandler = new HttpClientHandler();
118-
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
119-
120-
try
121-
{
122-
apiInstance.DataDelete();
123-
}
124-
catch (ApiException e)
125-
{
126-
Debug.Print("Exception when calling DefaultApi.DataDelete: " + e.Message );
127-
Debug.Print("Status Code: "+ e.ErrorCode);
128-
Debug.Print(e.StackTrace);
129-
}
13041

42+
Configuration config = new Configuration();
43+
config.BasePath = "https://push.databox.com";
44+
config.Username = "<Your_Databox_API_Token>";
45+
config.DefaultHeaders.Add("Accept", "application/vnd.databox.v2+json");
46+
47+
48+
HttpClient httpClient = new HttpClient();
49+
HttpClientHandler httpClientHandler = new HttpClientHandler();
50+
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
51+
var dataPostRequest = new List<PushData>() {
52+
new PushData() {
53+
Key = "<Metric_name>",
54+
Value = 123,
55+
Date = "<Date_in_ISO8601>",
56+
Unit = "<Unit>", // Optional
57+
Attributes = new List<PushDataAttribute>() { // Optional
58+
new PushDataAttribute() {
59+
Key = "<Dimension_name>",
60+
Value = "<Dimension_value>"
61+
}
62+
}
13163
}
64+
};
65+
66+
try
67+
{
68+
var response = await apiInstance.DataPostWithHttpInfoAsync(dataPostRequest);
69+
Console.WriteLine(response.Data.ToString());
70+
}
71+
catch (ApiException e)
72+
{
73+
Console.WriteLine("Exception when calling DefaultApi.DataPostWithHttpInfo: " + e.Message);
74+
Console.WriteLine("Status Code: " + e.ErrorCode);
75+
Console.WriteLine(e.StackTrace);
76+
}
13277
}
78+
}
13379
}
13480
```
135-
136-
<a id="documentation-for-api-endpoints"></a>
137-
## Documentation for API Endpoints
138-
139-
All URIs are relative to *https://push.databox.com*
140-
141-
Class | Method | HTTP request | Description
142-
------------ | ------------- | ------------- | -------------
143-
*DefaultApi* | [**DataDelete**](docs/DefaultApi.md#datadelete) | **DELETE** /data |
144-
*DefaultApi* | [**DataMetricKeyDelete**](docs/DefaultApi.md#datametrickeydelete) | **DELETE** /data/{metricKey} |
145-
*DefaultApi* | [**DataPost**](docs/DefaultApi.md#datapost) | **POST** /data |
146-
*DefaultApi* | [**MetrickeysGet**](docs/DefaultApi.md#metrickeysget) | **GET** /metrickeys |
147-
*DefaultApi* | [**MetrickeysPost**](docs/DefaultApi.md#metrickeyspost) | **POST** /metrickeys |
148-
*DefaultApi* | [**PingGet**](docs/DefaultApi.md#pingget) | **GET** /ping |
149-
150-
151-
<a id="documentation-for-models"></a>
152-
## Documentation for Models
153-
154-
- [Model.ApiResponse](docs/ApiResponse.md)
155-
- [Model.PushData](docs/PushData.md)
156-
- [Model.PushDataAttribute](docs/PushDataAttribute.md)
157-
- [Model.State](docs/State.md)
158-
159-
160-
<a id="documentation-for-authorization"></a>
161-
## Documentation for Authorization
162-
163-
164-
Authentication schemes defined for the API:
165-
<a id="basicAuth"></a>
166-
### basicAuth
167-
168-
- **Type**: HTTP basic authentication
169-

examples/PushData/Program.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Diagnostics;
2+
using Databox.Api;
3+
using Databox.Client;
4+
using Databox.Model;
5+
6+
namespace Example
7+
{
8+
public class Example
9+
{
10+
public static async Task Main(string[] args)
11+
{
12+
13+
Configuration config = new Configuration();
14+
config.BasePath = "https://push.databox.com";
15+
config.Username = "<Your_Databox_API_Token>";
16+
config.DefaultHeaders.Add("Accept", "application/vnd.databox.v2+json");
17+
18+
19+
HttpClient httpClient = new HttpClient();
20+
HttpClientHandler httpClientHandler = new HttpClientHandler();
21+
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
22+
var dataPostRequest = new List<PushData>() {
23+
new PushData() {
24+
Key = "<Metric_name>",
25+
Value = 123,
26+
Date = "<Date_in_ISO8601>",
27+
Unit = "<Unit>", // Optional
28+
Attributes = new List<PushDataAttribute>() { // Optional
29+
new PushDataAttribute() {
30+
Key = "<Dimension_name>",
31+
Value = "<Dimension_value>"
32+
}
33+
}
34+
}
35+
};
36+
37+
try
38+
{
39+
var response = await apiInstance.DataPostWithHttpInfoAsync(dataPostRequest);
40+
Console.WriteLine(response.Data.ToString());
41+
}
42+
catch (ApiException e)
43+
{
44+
Console.WriteLine("Exception when calling DefaultApi.DataPostWithHttpInfo: " + e.Message);
45+
Console.WriteLine("Status Code: " + e.ErrorCode);
46+
Console.WriteLine(e.StackTrace);
47+
}
48+
}
49+
}
50+
}

examples/PushData/PushData.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Databox" Version="0.0.3" />
12+
</ItemGroup>
13+
14+
</Project>

examples/PushData/PushData.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PushData", "PushData.csproj", "{4951769A-90A9-4B3F-84DD-2CFE74331CB8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1866E3A2-EC7E-48B8-9B4A-8E037749153E}
24+
EndGlobalSection
25+
EndGlobal

examples/PushData/nuget.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
5+
</packageSources>
6+
<activePackageSource>
7+
<add key="All" value="(Aggregate source)" />
8+
</activePackageSource>
9+
</configuration>

0 commit comments

Comments
 (0)