Skip to content

Commit

Permalink
Merge pull request #19 from databox/pushdata-example
Browse files Browse the repository at this point in the history
Added example
  • Loading branch information
bwiz authored Sep 18, 2024
2 parents 8d6b1d8 + e1ef703 commit b06ed3b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 140 deletions.
1 change: 0 additions & 1 deletion .github/workflows/generate_sdk_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ jobs:
run: |
java --version
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
cp ./src/README.md ./README.md
cp -r ./src/docs ./docs
- name: Create Pull Request
Expand Down
189 changes: 50 additions & 139 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,169 +1,80 @@
# Databox - the C# library for the Static OpenAPI document of Push API resource
## Databox

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

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

- API version: 0.4.4-alpha.4
- SDK version: 0.0.3
- Generator version: 7.6.0
- Build package: org.openapitools.codegen.languages.CSharpClientCodegen

<a id="frameworks-supported"></a>
## Frameworks supported

<a id="dependencies"></a>
## Dependencies

- [Json.NET](https://www.nuget.org/packages/Newtonsoft.Json/) - 13.0.2 or later
- [JsonSubTypes](https://www.nuget.org/packages/JsonSubTypes/) - 1.8.0 or later
- [System.ComponentModel.Annotations](https://www.nuget.org/packages/System.ComponentModel.Annotations) - 5.0.0 or later

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:
```
Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
Install-Package System.ComponentModel.Annotations
```
<a id="installation"></a>
## Installation
Run the following command to generate the DLL
- [Mac/Linux] `/bin/sh build.sh`
- [Windows] `build.bat`

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

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.
Databox package repository url is: https://nuget.pkg.github.com/databox/index.json

This `.nuspec` uses placeholders from the `.csproj`, so build the `.csproj` directly:
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).

```
nuget pack -Build -OutputDirectory out Databox.csproj
```

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.
After this is completed, the package can be installed via IDE (package named **"Databox"**) or via **dotnet cli** command

<a id="usage"></a>
## Usage

To use the API client with a HTTP proxy, setup a `System.Net.WebProxy`
```csharp
Configuration c = new Configuration();
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;
```

### Connections
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.

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.

```csharp
HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);
dotnet add package Databox --version <version>
```

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.
### Prerequisites

```csharp
HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient);
```
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.

Here an example of DI setup in a sample web project:

```csharp
services.AddHttpClient<YourApiClass>(httpClient =>
new PetApi(httpClient));
```
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.

### Example

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

```csharp
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using Databox.Api;
using Databox.Client;
using Databox.Model;

namespace Example
{
public class Example
public class Example
{
public static async Task Main(string[] args)
{
public static void Main()
{

Configuration config = new Configuration();
config.BasePath = "https://push.databox.com";
// Configure HTTP basic authorization: basicAuth
config.Username = "YOUR_USERNAME";
config.Password = "YOUR_PASSWORD";

// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
HttpClient httpClient = new HttpClient();
HttpClientHandler httpClientHandler = new HttpClientHandler();
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);

try
{
apiInstance.DataDelete();
}
catch (ApiException e)
{
Debug.Print("Exception when calling DefaultApi.DataDelete: " + e.Message );
Debug.Print("Status Code: "+ e.ErrorCode);
Debug.Print(e.StackTrace);
}

Configuration config = new Configuration();
config.BasePath = "https://push.databox.com";
config.Username = "<Your_Databox_API_Token>";
config.DefaultHeaders.Add("Accept", "application/vnd.databox.v2+json");


HttpClient httpClient = new HttpClient();
HttpClientHandler httpClientHandler = new HttpClientHandler();
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
var dataPostRequest = new List<PushData>() {
new PushData() {
Key = "<Metric_name>",
Value = 123,
Date = "<Date_in_ISO8601>",
Unit = "<Unit>", // Optional
Attributes = new List<PushDataAttribute>() { // Optional
new PushDataAttribute() {
Key = "<Dimension_name>",
Value = "<Dimension_value>"
}
}
}
};

try
{
var response = await apiInstance.DataPostWithHttpInfoAsync(dataPostRequest);
Console.WriteLine(response.Data.ToString());
}
catch (ApiException e)
{
Console.WriteLine("Exception when calling DefaultApi.DataPostWithHttpInfo: " + e.Message);
Console.WriteLine("Status Code: " + e.ErrorCode);
Console.WriteLine(e.StackTrace);
}
}
}
}
```

<a id="documentation-for-api-endpoints"></a>
## Documentation for API Endpoints

All URIs are relative to *https://push.databox.com*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*DefaultApi* | [**DataDelete**](docs/DefaultApi.md#datadelete) | **DELETE** /data |
*DefaultApi* | [**DataMetricKeyDelete**](docs/DefaultApi.md#datametrickeydelete) | **DELETE** /data/{metricKey} |
*DefaultApi* | [**DataPost**](docs/DefaultApi.md#datapost) | **POST** /data |
*DefaultApi* | [**MetrickeysGet**](docs/DefaultApi.md#metrickeysget) | **GET** /metrickeys |
*DefaultApi* | [**MetrickeysPost**](docs/DefaultApi.md#metrickeyspost) | **POST** /metrickeys |
*DefaultApi* | [**PingGet**](docs/DefaultApi.md#pingget) | **GET** /ping |


<a id="documentation-for-models"></a>
## Documentation for Models

- [Model.ApiResponse](docs/ApiResponse.md)
- [Model.PushData](docs/PushData.md)
- [Model.PushDataAttribute](docs/PushDataAttribute.md)
- [Model.State](docs/State.md)


<a id="documentation-for-authorization"></a>
## Documentation for Authorization


Authentication schemes defined for the API:
<a id="basicAuth"></a>
### basicAuth

- **Type**: HTTP basic authentication

50 changes: 50 additions & 0 deletions examples/PushData/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Diagnostics;
using Databox.Api;
using Databox.Client;
using Databox.Model;

namespace Example
{
public class Example
{
public static async Task Main(string[] args)
{

Configuration config = new Configuration();
config.BasePath = "https://push.databox.com";
config.Username = "<Your_Databox_API_Token>";
config.DefaultHeaders.Add("Accept", "application/vnd.databox.v2+json");


HttpClient httpClient = new HttpClient();
HttpClientHandler httpClientHandler = new HttpClientHandler();
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
var dataPostRequest = new List<PushData>() {
new PushData() {
Key = "<Metric_name>",
Value = 123,
Date = "<Date_in_ISO8601>",
Unit = "<Unit>", // Optional
Attributes = new List<PushDataAttribute>() { // Optional
new PushDataAttribute() {
Key = "<Dimension_name>",
Value = "<Dimension_value>"
}
}
}
};

try
{
var response = await apiInstance.DataPostWithHttpInfoAsync(dataPostRequest);
Console.WriteLine(response.Data.ToString());
}
catch (ApiException e)
{
Console.WriteLine("Exception when calling DefaultApi.DataPostWithHttpInfo: " + e.Message);
Console.WriteLine("Status Code: " + e.ErrorCode);
Console.WriteLine(e.StackTrace);
}
}
}
}
14 changes: 14 additions & 0 deletions examples/PushData/PushData.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Databox" Version="0.0.3" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions examples/PushData/PushData.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PushData", "PushData.csproj", "{4951769A-90A9-4B3F-84DD-2CFE74331CB8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4951769A-90A9-4B3F-84DD-2CFE74331CB8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1866E3A2-EC7E-48B8-9B4A-8E037749153E}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions examples/PushData/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
</configuration>

0 comments on commit b06ed3b

Please sign in to comment.