Example: Basic Call for REST Services

The following assembly references are required:

  • mscorlib
  • System
  • System.Core
  • System.Net.Http
  • System.Net.Http.Formatting
  • System.Web

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
 
namespace Examples.CSharp.Rest
{
    /// <summary>   A REST example. </summary>
    public static partial class RestExample
    {
        /// <summary> Example of basic call. </summary>
        /// <remarks>
        /// Shows how to call the service by submitting a simple data structure as parameters via GET.
        /// </remarks>
        /// <exception cref="InvalidOperationException">    Thrown when the requested operation is
        ///                                                 invalid. </exception>
        /// <param name="authToken"> The authentication token. </param>
        public static void BasicCall(string authToken)
        {
            // Create parameter query as expected by the TecRMI REST API
            var parameterQuery = HttpUtility.ParseQueryString(string.Empty);
            parameterQuery.Add("LanguageCode", "en");
 
            HttpResponseMessage response;
 
            using (var serviceClient = new HttpClient())
            {
                // Set the media type for the response either JSON ("application/json") or XML ("application/xml")
                // If not set the default response will be "application/json".
                serviceClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 
                // Set the authentication token, the user agent and the origin.
                // These have to be the same for all requests.
                serviceClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("TecRMI", authToken);
 
                // Set the user agent to a value that indentifies your client application. If your client application is also
                // a web application the user agent of the original request could be passed to identify the user's browser
                // e.g. new ProductInfoHeaderValue(HttpContext.Current.Request.UserAgent).
                serviceClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Examples.Csharp.Rest", "1.0"));
 
                // Set the origin to a value that indentifies your client application. If your client application is also
                // a web application the user agent of the original request could be passed (HttpContext.Current.Request.UserAgent)
                // to identify the user's browser.
                serviceClient.DefaultRequestHeaders.Add("Origin", "http://origin.example");
 
                // Create URI
                var uriBuilder = new UriBuilder("https://rmi-services.tecalliance.net")
                {
                    Port = -1, // suppress explicit port qualifier
                    Path = "/rest/common/languages",
                    Query = parameterQuery.ToString(),
                };
 
                // Call service via GET.
                // Instead of using a UriBuilder, you can also pass a simple URI string
                // e.g. "/rest/common/languages?" + parameterQuery.ToString()
                response = serviceClient.GetAsync(uriBuilder.Uri).Result;
            }
 
            // response type
            Language[] languages;
 
            using (response)
            {
                // handle the error if necessary.
                if (!response.IsSuccessStatusCode)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.Unauthorized: // 401
                            Console.WriteLine("Not Authenticated!");
                            return;
                        case HttpStatusCode.Forbidden: // 403
                            Console.WriteLine("Unsufficient permissions!");
                            return;
                        case HttpStatusCode.InternalServerError: // 500
                            Console.WriteLine("An error occured!");
                            return;
                        default:
                            Console.WriteLine("Unexpected response!");
                            return;
                    }
                }
 
                languages = response.Content.ReadAsAsync<Language[]>().Result;
            }
 
            // Process the response
            Console.WriteLine(" Id | Code | Name");
            Console.WriteLine("----+------+------------");
            foreach (var lang in languages)
            {
                Console.WriteLine(
                    " {0,2} | {1,-4} | {2}",
                    lang.LanguageId,
                    lang.LanguageCode,
                    lang.LanguageName);
            }
        }
    }
 
    /// <summary>
    /// Class <c>Language</c> represents a language in the AuDaLib and contains the important
    /// informations like the <c>id</c>, <c>name</c>, etc.
    /// </summary>
    public class Language
    {
        /// <summary>
        /// Gets or sets the id of the language.
        /// </summary>
        /// <value>
        /// The language id.
        /// </value>
        public int LanguageId { get; set; }
 
        /// <summary>
        /// Gets or sets the 2-letter international language code as defined by ISO 639-1.
        /// </summary>
        /// <value>
        /// The language code.
        /// </value>
        public string LanguageCode { get; set; }
 
        /// <summary>
        /// Gets or sets the name of the language.
        /// </summary>
        /// <value>
        /// The name of the language.
        /// </value>
        public string LanguageName { get; set; }
 
        /// <summary>
        /// Gets or sets a value determining whether all texts are available in this language.
        /// </summary>
        /// <value>
        /// The value determining whether the language is complete.
        /// </value>
        public int IsCompleteFilled { get; set; }
    }
}
 
©   TecAlliance GmbH