Wiki source code of Setting up API communication by REST services with TerraIndex for customers
Last modified by Robert Jan Daams on 2025/02/04 11:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}} | ||
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
5 | = Setting up an API account = | ||
6 | |||
7 | Setting up an API account is as easy as inviting a colleague to collaborate with in TerraIndex. | ||
8 | |||
9 | Just make sure the "API account" checkbox is checked, and a representative username is chosen. | ||
10 | |||
11 | |||
12 | [[image:1738665885135-957.png]] | ||
13 | |||
14 | {{info}} | ||
15 | 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. | ||
16 | {{/info}} | ||
17 | |||
18 | [[image:1738665966138-176.png]] | ||
19 | |||
20 | = Usefull code snippets = | ||
21 | |||
22 | == C# .NET == | ||
23 | |||
24 | == Zip Stream class == | ||
25 | |||
26 | {{info}} | ||
27 | This is a Zip Stream class you can use to extract a zipstream. | ||
28 | {{/info}} | ||
29 | |||
30 | {{code language="Csharp" layout="LINENUMBERS"}} | ||
31 | /// <summary> | ||
32 | /// Helper class for compress en decompress of file data | ||
33 | /// </summary> | ||
34 | public static class ZipStreamHelper { | ||
35 | #region Compress and encode | ||
36 | /// <summary> | ||
37 | /// Comprimeer een string en geeft deze terug in Base64 string. | ||
38 | /// </summary> | ||
39 | /// <param name="content">filecontent</param> | ||
40 | /// <returns>gecomprimeerde string</returns> | ||
41 | public static string CompressToBase64String(string content) { | ||
42 | //string to byte[] | ||
43 | byte[] contentArray = stringToByteArray(content); | ||
44 | // Compress | ||
45 | byte[] compressed = Compress(contentArray); | ||
46 | return base64_encode(compressed); | ||
47 | } | ||
48 | |||
49 | /// <summary> | ||
50 | /// Zet een string om naar een ByteArray | ||
51 | /// </summary> | ||
52 | /// <param name="content">filecontent</param> | ||
53 | /// <returns>ByteArray</returns> | ||
54 | public static byte[] stringToByteArray(string content) { | ||
55 | System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); | ||
56 | return encoding.GetBytes(content); | ||
57 | } | ||
58 | |||
59 | /// <summary> | ||
60 | /// comprimeer een ByteArray | ||
61 | /// </summary> | ||
62 | /// <param name="data">filedata als ByteArray</param> | ||
63 | /// <returns>gecomprimeerde ByteArray</returns> | ||
64 | public static byte[] Compress(byte[] data) { | ||
65 | using(var compressedStream = new MemoryStream()) | ||
66 | using(var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) { | ||
67 | zipStream.Write(data, 0, data.Length); | ||
68 | zipStream.Close(); | ||
69 | return compressedStream.ToArray(); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | /// <summary> | ||
74 | /// Zet een ByteArray om naar een Base64 string | ||
75 | /// </summary> | ||
76 | /// <param name="data">gecomprimeerde ByteArray</param> | ||
77 | /// <returns>Base 64 string</returns> | ||
78 | public static string base64_encode(byte[] data) { | ||
79 | if (data == null) | ||
80 | return string.Empty; | ||
81 | return Convert.ToBase64String(data); | ||
82 | } | ||
83 | |||
84 | /// <summary> | ||
85 | /// Zet een string om naar een Base64 string | ||
86 | /// </summary> | ||
87 | /// <param name="data">string</param> | ||
88 | /// <returns>Base 64 string</returns> | ||
89 | public static string string_base64_encode(string data) { | ||
90 | if (string.IsNullOrEmpty(data)) | ||
91 | return string.Empty; | ||
92 | |||
93 | //string to byte[] | ||
94 | byte[] contentArray = stringToByteArray(data); | ||
95 | return base64_encode(contentArray); | ||
96 | } | ||
97 | #endregion | ||
98 | |||
99 | #region Decompress and decode | ||
100 | // Decode and decompress | ||
101 | /// <summary> | ||
102 | /// Decomprimeer een Base64 string naar een string | ||
103 | /// </summary> | ||
104 | /// <param name="contentBase64">GZIP Base64 string</param> | ||
105 | /// <returns>string</returns> | ||
106 | public static string DecompressBase64StringToString(string contentBase64) { | ||
107 | // Decompress | ||
108 | byte[] decoded = base64_decode(contentBase64); | ||
109 | byte[] decompressed = Decompress(decoded); | ||
110 | |||
111 | return byteArrayTostring(decompressed); | ||
112 | } | ||
113 | |||
114 | /// <summary> | ||
115 | /// Zet een ByteArray om in een normale string | ||
116 | /// </summary> | ||
117 | /// <param name="data">ByteArray</param> | ||
118 | /// <returns>string</returns> | ||
119 | public static string byteArrayTostring(byte[] data) { | ||
120 | System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); | ||
121 | return enc.GetString(data); | ||
122 | } | ||
123 | |||
124 | /// <summary> | ||
125 | /// Zet een Base64 string om naar een ByteArray | ||
126 | /// </summary> | ||
127 | /// <param name="encodedData">Base64 string</param> | ||
128 | /// <returns>gecomprimeerde ByteArray</returns> | ||
129 | public static byte[] base64_decode(string encodedData) { | ||
130 | byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData); | ||
131 | return encodedDataAsBytes; | ||
132 | } | ||
133 | |||
134 | /// <summary> | ||
135 | /// Zet een gecomprimeerde ByteArray om in een ByteArray | ||
136 | /// </summary> | ||
137 | /// <param name="data">gecomprimeerde ByteArray</param> | ||
138 | /// <returns>ByteArray (gedecomprimeerd)</returns> | ||
139 | public static byte[] Decompress(byte[] data) { | ||
140 | using(var compressedStream = new MemoryStream(data)) | ||
141 | using(var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) | ||
142 | using(var resultStream = new MemoryStream()) { | ||
143 | var buffer = new byte[4096]; | ||
144 | int read; | ||
145 | |||
146 | while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0) { | ||
147 | resultStream.Write(buffer, 0, read); | ||
148 | } | ||
149 | |||
150 | return resultStream.ToArray(); | ||
151 | } | ||
152 | } | ||
153 | #endregion | ||
154 | } | ||
155 | {{/code}} |