TaxonomyImportModel

View as Markdown

TaxonomyImportModel

TaxonomyImportModel wraps a JSON or CSV taxonomy file for the Import and ImportAsync methods. Build it from a file path or from a stream.

When to Use This Class: Use this class to create a taxonomy and its terms from one file instead of one request per term.

Conceptual Role: Acts as the upload body of the Taxonomy import request. It implements IUploadInterface, which the SDK calls to build the multipart/form-data content.

Returns: TaxonomyImportModel. A taxonomy file ready for upload.

The class has two constructors:

NameTypeDescription
filePathstringPath of the file. The constructor opens it for reading.
streamStreamStream that holds the file content.
fileNamestringFile name for the upload when you pass stream. Defaults to "taxonomy.json".
  • new TaxonomyImportModel(string filePath) opens the file at filePath and uses its file name for the upload.
  • new TaxonomyImportModel(Stream stream, string fileName = "taxonomy.json") uses your stream and the name you pass. A null fileName also becomes "taxonomy.json".

Properties

NameTypeRequiredDefaultDescription
ContentTypestringNo"multipart/form-data"Content type that the SDK sends for the upload.

Class-Level Notes

Constructor checks.

  • The path constructor throws ArgumentNullException (parameter filePath) when filePath is null or an empty string.

The path constructor opens the file with File.OpenRead. A missing file or folder throws FileNotFoundException or DirectoryNotFoundException, and a file you cannot read throws UnauthorizedAccessException.

  • The stream constructor throws ArgumentNullException (parameter stream) when stream is null.

One model per upload.

  • The SDK reads the stream from its current position to the end when it sends the request, and does not rewind or close it.
  • Create a new TaxonomyImportModel for each Import call. A second call with the same model sends the rest of a stream that the first call already read.
  • The path constructor opens a file stream that the SDK never closes. To close the file after the import, open it yourself in a using block and pass the stream to the stream constructor.

Upload format.

  • The SDK sends the file in a form field named taxonomy, as the Import a taxonomy request in the Content Management API reference describes.
  • The SDK does not read or check the file content. Leave ContentType at its default.
  • GetHttpContent() builds the form content. The SDK calls it while it sends the request, so your code does not need to call it.

Method Index

TaxonomyImportModel has no methods for you to call. Build it with a constructor and pass it to the Import method.

Class-Level Snippet

The snippet below imports a taxonomy file and closes the file after the request.

using System;
using System.IO;
using System.Text.Json.Nodes;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

using FileStream file = File.OpenRead("<FILE_PATH>");
TaxonomyImportModel model = new TaxonomyImportModel(file, Path.GetFileName("<FILE_PATH>"));

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Taxonomy().Import(model);
    JsonObject json = response.OpenJsonObjectResponse();
    Console.WriteLine($"Imported {json["taxonomy"]?["uid"]} with {json["taxonomy"]?["terms_count"]} terms");
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request, for example a file in an unsupported shape
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}