C#

OpenAPI Specificationから生成 されたC#用クライアントライブラリを使用するサンプルを示します。

APIトークンを使用してAPIにアクセスする

以下のコードでは、APIトークンを使用して認証を行います。その後、認証したユーザー自身の情報を取得します。

実行時には以下を指定してください。

  • TARGET_SERVER: 接続先intdashサーバー名

  • API_TOKEN: ユーザーのAPIトークン(APIトークンはMy Pageで作成できます)

using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

// intdashクライアントの名前空間宣言。
using intdash.Api;
using intdash.Client;
using intdash.Model;

public partial class ExampleUnityScript : MonoBehaviour
{
    /// <summary>
    /// intdashサーバー名
    /// </summary>
    const string TARGET_SERVER = "https://example.com"

    /// <summary>
    /// ユーザーのAPIトークン(APIトークンはMy Pageで作成できます)
    /// </summary>
    const string API_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    /// <summary>
    /// APIの設定です。
    /// <para>アクセス情報やトークンをセットします</para>
    /// </summary>
    Configuration apiConfig = new Configuration();

    async void ApiAccessExample()
    {
        try
        {
            // 接続先をセットします。
            apiConfig.BasePath = TARGET_SERVER + "/api";
            // ユーザーのAPIトークンは以下のようにセットしてください。
            apiConfig.AddApiKey("X-Intdash-Token", API_TOKEN);

            // 任意のAPIを実行します。
            var user = await (new AuthMeApi(apiConfig)).GetMeAsync().ConfigureAwait(false);
            Debug.Log($"User name: {user.Name}, uuid: {user.Uuid}");
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to getMe access. {e.Message}");
        }
    }
}

OAuth2トークンを使用してAPIにアクセスする

以下のコードでは、OAuth2トークンを使って認証を行います。その後、認証したユーザー自身の情報を取得します。

実行時には以下を指定してください。

  • TARGET_SERVER: 接続先intdashサーバー名

  • CLIENT_ID: エッジのUUID

  • CLIENT_SECRET: エッジの作成時に発行されるクライアントシークレット

public partial class ExampleUnityScript : MonoBehaviour
{
    /// <summary>
    /// クライアントID
    /// </summary>
    const string CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    /// <summary>
    /// エッジの作成時に発行されるクライアントシークレット
    /// </summary>
    const string CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    async void ApiAccessExampleWithOAuth2Token()
    {
        try
        {
            // 接続先をセットします。
            apiConfig.BasePath = TARGET_SERVER + "/api";

            // OAuth2トークンをリクエストします。
            var response = await (new AuthOAuth2Api(apiConfig)).IssueTokenAsync(
                grantType: "client_credentials",
                clientId: CLIENT_ID,
                clientSecret: CLIENT_SECRET).ConfigureAwait(false);

            // 取得したアクセストークンを以下のようにセットして下さい。
            // 以降、トークンの有効期限が切れない回切り再設定は不要です。
            apiConfig.AccessToken = response.AccessToken;

            // 任意のAPIを実行します。
            var user = await (new AuthEdgesApi(apiConfig)).GetMeAsEdgeAsync().ConfigureAwait(false);
            Debug.Log($"Edge name: {user.Name}, uuid: {user.Uuid}");
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to getMe access. {e.Message}");
        }
    }
}

過去の計測のデータポイントを取得する

データポイントを取得する場合、自動生成されたコードはそのままでは使用できず、一部を修正する必要があります。 パッチの例を以下に示します。

diff --git a/OpenAPIClientSample/Assets/Plugins/intdash/Model/DataPointNormal.cs b/OpenAPIClientSample/Assets/Plugins/intdash/Model/DataPointNormal.cs
index 525f4ae..65c013b 100644
--- a/OpenAPIClientSample/Assets/Plugins/intdash/Model/DataPointNormal.cs
+++ b/OpenAPIClientSample/Assets/Plugins/intdash/Model/DataPointNormal.cs
@@ -46,7 +46,7 @@ namespace intdash.Model
        /// <param name="dataType">データタイプ (required).</param>
        /// <param name="dataId">データID (required).</param>
        /// <param name="data">データのペイロード。ペイロードのJSON表現はデータタイプによって異なります。 [詳説iSCP 1.0](https://docs.intdash.jp/manual/iscp1-essentials/latest/ja/iscp1-essentials-ja.pdf) の「付録: データタイプとペイロードの定義」を参照してください。 iscpv1に当てはまらない場合、下記のフォーマットで固定となります。  &#x60;&#x60;&#x60; {   \&quot;d\&quot;: \&quot;データ本体のBase64表記\&quot; } &#x60;&#x60;&#x60; (required).</param>
-        public DataPointNormal(DataPointTime time = default(DataPointTime), string measurementUuid = default(string), string createdAt = default(string), string dataType = default(string), string dataId = default(string), Object data = default(Object))
+        public DataPointNormal(DateTime time = default(DateTime), string measurementUuid = default(string), string createdAt = default(string), string dataType = default(string), string dataId = default(string), Object data = default(Object))
         {
             // to ensure "time" is required (not null)
             if (time == null)
@@ -90,7 +90,7 @@ namespace intdash.Model
         /// Gets or Sets Time
         /// </summary>
         [DataMember(Name = "time", IsRequired = true, EmitDefaultValue = false)]
-        public DataPointTime Time { get; set; }
+        public DateTime Time { get; set; }

         /// <summary>
         /// このデータポイントが含まれる計測のUUID
         /// </summary>
         /// <value>このデータポイントが含まれる計測のUUID</value>

上記のような修正を行ったうえで、以下のようにデータポイントを取得します。

public partial class ExampleUnityScript : MonoBehaviour
{
    async Task<(DataPointNormal[], Exception)> RequestListDataPoints(string name, List<string> filters, DateTime start, DateTime end, long? limit = null, string order = null)
    {
        try
        {
            var sStart = default(string);
            if (start != null)
            {
                sStart = start.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFZ");
            }
            var sEnd = default(string);
            if (end != null)
            {
                sEnd = end.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFZ");
            }
            // MeasDataPointsApi.ListDataPointsAPIにアクセスします。
            var fp = await (new MeasDataPointsApi(apiConfig)).ListDataPointsAsync(
                name: name,
                start: sStart,
                end: sEnd,
                idq: filters,
                limit: limit,
                order: order,
                timeFormat: "rfc3339").ConfigureAwait(false);
            var points = new List<DataPointNormal>();
            using (var reader = new StreamReader(fp.Content, Encoding.UTF8))
            {
                while (!reader.EndOfStream)
                {
                    try
                    {
                        var json = reader.ReadLine();
                        var point = JsonConvert.DeserializeObject<DataPointNormal>(json);
                        points.Add(point);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError($"Failed to parse json. {e.Message}");
                        continue;
                    }
                }
            }
            return (points.ToArray(), null);
        }
        catch (Exception e)
        {
            return (null, e);
        }
    }
}