fbpx
印刷

C#を使ってBuildVuマイクロサービスにアクセスする

はじめに

以下のチュートリアルでは、ホストされたBuildVuクラウドAPIを使ってPDFファイルをHTMLやSVGに変換する方法を紹介します:

上記のサービスはすべて普通のHTTPリクエストでアクセスできますが、このチュートリアルでは REST APIをシンプルなC#ラッパーで提供するオープンソースのC# IDRCloudClient を使用します。

前提条件

nugetを使用して、次のコマンドでidrsolutions-csharp-clientパッケージをインストールします:

				
					nuget install idrsolutions-csharp-client
				
			

サンプルコード

PDFファイルをHTMLやSVGに変換する基本的なコード例です。設定オプションと詳細機能は下記にあります。

				
					using System;
using System.Collections.Generic;
using idrsolutions-csharp-client;

class ExampleUsage
{
    static void Main(string[] args)
    {
        
        var client = new IDRCloudClient("https://cloud.idrsolutions.com/cloud/" + IDRCloudClient.BUILDVU);

        try
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>
            {
                //["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
                ["input"] = IDRCloudClient.UPLOAD,
                ["file"] = "path/to/input.pdf"
            };

            Dictionary<string, string> results = client.Convert(parameters);

            String outputUrl = results.GetValueOrDefault("downloadUrl", "No download URL provided");
            
            client.DownloadResult(results, "path/to/output/dir");

            Console.WriteLine("Converted: " + outputUrl);
        }
        catch (Exception e)
        {
            Console.WriteLine("Conversion failed: " + e.Message);
        }
    }
}
				
			

コールバックURLへ結果を返す

BuildVu Microserviceは、完了時に変換ステータスを送信するコールバックURLを受け付けます。コールバックURLを使用すると、変換がいつ完了したかを判断するためにサービスをポーリングする必要がなくなります。
convert methodでは、以下のようにコールバックURLを指定することができます。

				
					Dictionary<string, string> parameters = new Dictionary<string, string>
{
    //["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
    ["callbackUrl"] = "http://listener.url",
    ["input"] = IDRCloudClient.UPLOAD,
    ["file"] = "path/to/input.pdf"
};

				
			

設定オプション

BuildVu APIは、変換をカスタマイズするためのキーバリューペア設定オプションを含む文字列化されたJSONオブジェクトを受け付けます。この設定はconvertメソッドに与える必要があります。PDFファイルをHTMLまたはSVGに変換するための設定オプションの完全なリストはこちらをご覧ください。

				
					["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"
				
			

URLによるアップロード

ローカルファイルをアップロードするだけでなく、BuildVu Microserviceがダウンロードして変換を実行するURLを指定することもできます。これを行うには、convertメソッドのinputとfileの値を以下のように置き換えます。

				
					["input"] = IDRCloudClient.DOWNLOAD
["url"] = "http://exampleURL/exampleFile.pdf"
				
			

認証を使用する

PDFファイルをHTMLまたはSVGに変換するためにユーザー名とパスワードを必要とする独自のBuildVu Microserviceをデプロイした場合、変換のたびにユーザー名とパスワードを提供する必要があります。これらは、以下のようにconvertメソッドにauthという変数を渡すことで提供されます。

				
					var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.BUILDVU, "username", "password");

				
			
    MENU
    PAGE TOP