/// <summary>
        /// Registers an application through the task management REST API as an asynchronous operation.
        /// </summary>
        /// <param name="taskManagementUrl">The url of the TaskManagement component.</param>
        /// <param name="requestData">Contains the necessary information for registering an application.</param>
        /// <returns>True if the request was successful.</returns>
        public static async Task RegisterApplicationAsync(string taskManagementUrl, RegisterApplicationRequest requestData)
        {
            // null checks
            if (string.IsNullOrEmpty(taskManagementUrl))
            {
                throw new TaskManagementException(Error.REGISTERTASK_MISSING_URL);
            }
            if (requestData == null)
            {
                throw new TaskManagementException(Error.REGISTERTASK_MISSING_REQUESTDATA);
            }
            if (string.IsNullOrEmpty(requestData.AppId))
            {
                throw new TaskManagementException(Error.REGISTERAPP_MISSING_APPID);
            }

            HttpResponseMessage response = null;
            var responseText             = string.Empty;

            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

            try
            {
                response = await SendRequest(taskManagementUrl, requestData, APIURL_REGISTERAPP).ConfigureAwait(false);

                responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    var result = JsonConvert.DeserializeObject <RegisterApplicationResult>(responseText);
                    if (!string.IsNullOrEmpty(result?.Error))
                    {
                        throw new TaskManagementException(result.Error,
                                                          requestData.AppId, null);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                throw new TaskManagementException(Error.REGISTERAPP + GetResponsePhrase(response) + responseText, requestData.AppId, ex);
            }

            // the response did not throw an exception, but was not successful
            throw new TaskManagementException(Error.REGISTERAPP + GetResponsePhrase(response) + responseText, requestData.AppId, null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers an application through the task management REST API as an asynchronous operation.
        /// </summary>
        /// <param name="taskManagementUrl">The url of the TaskManagement component.</param>
        /// <param name="requestData">Contains the necessary information for registering an application.</param>
        /// <returns>True if the request was successful.</returns>
        public static async Task RegisterApplicationAsync(string taskManagementUrl, RegisterApplicationRequest requestData)
        {
            // null checks
            if (string.IsNullOrEmpty(taskManagementUrl))
            {
                throw new TaskManagementException(Error.REGISTERTASK_MISSING_URL);
            }
            if (requestData == null)
            {
                throw new TaskManagementException(Error.REGISTERTASK_MISSING_REQUESTDATA);
            }
            if (string.IsNullOrEmpty(requestData.AppId))
            {
                throw new TaskManagementException(Error.REGISTERAPP_MISSING_APPID);
            }

            HttpResponseMessage response = null;
            var responseText             = string.Empty;

            try
            {
                response = await SendRequest(taskManagementUrl, requestData, APIURL_REGISTERAPP).ConfigureAwait(false);

                responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                throw new TaskManagementException(Error.REGISTERAPP + GetResponsePhrase(response) + responseText, requestData.AppId, ex);
            }

            // the response did not throw an exception, but was not successful
            throw new TaskManagementException(Error.REGISTERAPP + GetResponsePhrase(response) + responseText, requestData.AppId, null);
        }