Пример #1
0
        } // End of the UploadFile method

        /// <summary>
        /// Download a file
        /// </summary>
        public async Task<FortnoxResponse<bool>> DownloadFile(Stream stream, string uri)
        {
            // Create the response to return
            FortnoxResponse<bool> fr = new FortnoxResponse<bool>();

            // Indicate success
            fr.model = true;

            try
            {
                // Get the response
                HttpResponseMessage response = await this.client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

                // Check the status code for the response
                if (response.IsSuccessStatusCode == true)
                {
                    // Get the stream
                    await response.Content.CopyToAsync(stream);
                }
                else
                {
                    // Get string data
                    string data = await response.Content.ReadAsStringAsync();

                    // Add error data
                    fr.model = false;
                    fr.error = $"DownloadFile: {uri}. {Regex.Unescape(data)}";
                }
            }
            catch (Exception ex)
            {
                // Add exception data
                fr.model = false;
                fr.error = $"DownloadFile: {uri}. {ex.ToString()}";
            }

            // Return the response
            return fr;

        } // End of the DownloadFile method
Пример #2
0
        } // End of the Action method

        #endregion

        #region Get methods

        /// <summary>
        /// Get a list with posts or just a post
        /// </summary>
        public async Task<FortnoxResponse<R>> Get<R>(string uri)
        {
            // Create the response to return
            FortnoxResponse<R> fr = new FortnoxResponse<R>();

            try
            {
                // Get the response
                HttpResponseMessage response = await this.client.GetAsync(uri);

                // Check the status code for the response
                if (response.IsSuccessStatusCode == true)
                {
                    // Get string data
                    string data = await response.Content.ReadAsStringAsync();

                    // Deserialize the data
                    fr.model = JsonConvert.DeserializeObject<R>(data);
                }
                else
                {
                    // Get string data
                    string data = await response.Content.ReadAsStringAsync();

                    // Add error data
                    fr.error = $"Get: {uri}. {Regex.Unescape(data)}";
                }
            }
            catch (Exception ex)
            {
                // Add exception data
                fr.error = $"Get: {uri}. {ex.ToString()}";
            }

            // Return the post
            return fr;

        } // End of the Get method