Implementatie documentatie - Setting up API communication by REST services with TerraIndex for customers 

Page / - last modified by Robert Jan Daams on 2025/02/04 11:46 

Setting up API communication by REST services with TerraIndex for customers 
last modified by Robert Jan Daams
on 2025/02/04 11:46

Table of Contents
Setting up an API account
Usefull code snippets
C# .NET
Zip Stream class

Contents
•	Setting up an API account
•	Usefull code snippets
•	C# .NET
•	Zip Stream class
Setting up an API account
Setting up an API account is as easy as inviting a colleague to collaborate with in TerraIndex.
Just make sure the "API account" checkbox is checked, and a representative username is chosen.

After saving the new account, and opening the new account again, the "Pin code" field will be available for you to generate your own API key.

Usefull code snippets
C# .NET
Zip Stream class
This is a Zip Stream class you can use to extract a zipstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/// <summary>
/// Helper class for compress en decompress of file data
/// </summary>
public static class ZipStreamHelper {
 #region Compress and encode
 /// <summary>
 /// Comprimeer een string en geeft deze terug in Base64 string.
 /// </summary>
 /// <param name="content">filecontent</param>
 /// <returns>gecomprimeerde string</returns>
 public static string CompressToBase64String(string content) {
   //string to byte[]
   byte[] contentArray = stringToByteArray(content);
   // Compress
   byte[] compressed = Compress(contentArray);
   return base64_encode(compressed);
  }

 /// <summary>
 /// Zet een string om naar een ByteArray
 /// </summary>
 /// <param name="content">filecontent</param>
 /// <returns>ByteArray</returns>
 public static byte[] stringToByteArray(string content) {
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
   return encoding.GetBytes(content);
  }

 /// <summary>
 /// comprimeer een ByteArray
 /// </summary>
 /// <param name="data">filedata als ByteArray</param>
 /// <returns>gecomprimeerde ByteArray</returns>
 public static byte[] Compress(byte[] data) {
    using(var compressedStream = new MemoryStream())
    using(var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) {
      zipStream.Write(data, 0, data.Length);
      zipStream.Close();
     return compressedStream.ToArray();
    }
  }

 /// <summary>
 /// Zet een ByteArray om naar een Base64 string
 /// </summary>
 /// <param name="data">gecomprimeerde ByteArray</param>
 /// <returns>Base 64 string</returns>
 public static string base64_encode(byte[] data) {
   if (data == null)
     return string.Empty;
   return Convert.ToBase64String(data);
  }

 /// <summary>
 /// Zet een string om naar een Base64 string
 /// </summary>
 /// <param name="data">string</param>
 /// <returns>Base 64 string</returns>
 public static string string_base64_encode(string data) {
   if (string.IsNullOrEmpty(data))
     return string.Empty;

   //string to byte[]
   byte[] contentArray = stringToByteArray(data);
   return base64_encode(contentArray);
  }
 #endregion

 #region Decompress and decode
 // Decode and decompress
 /// <summary>
 /// Decomprimeer een Base64 string naar een string
 /// </summary>
 /// <param name="contentBase64">GZIP Base64 string</param>
 /// <returns>string</returns>
 public static string DecompressBase64StringToString(string contentBase64) {
   // Decompress
   byte[] decoded = base64_decode(contentBase64);
   byte[] decompressed = Decompress(decoded);

   return byteArrayTostring(decompressed);
  }

 /// <summary>
 /// Zet een ByteArray om in een normale string
 /// </summary>
 /// <param name="data">ByteArray</param>
 /// <returns>string</returns>
 public static string byteArrayTostring(byte[] data) {
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
   return enc.GetString(data);
  }

 /// <summary>
 /// Zet een Base64 string om naar een ByteArray
 /// </summary>
 /// <param name="encodedData">Base64 string</param>
 /// <returns>gecomprimeerde ByteArray</returns>
 public static byte[] base64_decode(string encodedData) {
   byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
   return encodedDataAsBytes;
  }

 /// <summary>
 /// Zet een gecomprimeerde ByteArray om in een ByteArray
 /// </summary>
 /// <param name="data">gecomprimeerde ByteArray</param>
 /// <returns>ByteArray (gedecomprimeerd)</returns>
 public static byte[] Decompress(byte[] data) {
    using(var compressedStream = new MemoryStream(data))
    using(var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    using(var resultStream = new MemoryStream()) {
     var buffer = new byte[4096];
     int read;

     while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0) {
        resultStream.Write(buffer, 0, read);
      }

     return resultStream.ToArray();
    }
  }
 #endregion
}