2020-03-24 08:47:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2020-03-24 22:26:02 +00:00
|
|
|
|
using System.Net;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Net.Mime;
|
|
|
|
|
using System.Security.Authentication;
|
2020-03-24 22:26:02 +00:00
|
|
|
|
using System.Text;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-24 22:26:02 +00:00
|
|
|
|
using System.Web;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using FarmmapsApi.Models;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
using Google.Apis.Http;
|
|
|
|
|
using Google.Apis.Upload;
|
2020-03-24 22:26:02 +00:00
|
|
|
|
using IdentityModel;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
using Winista.Mime;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
namespace FarmmapsApi.Services
|
|
|
|
|
{
|
|
|
|
|
public class FarmmapsApiService
|
|
|
|
|
{
|
2020-03-25 20:32:28 +00:00
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
|
private readonly HttpClientSettings _httpClientSettings;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
private readonly OpenIdConnectService _openIdConnectService;
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
2020-03-25 20:32:28 +00:00
|
|
|
|
public FarmmapsApiService(HttpClientSettings httpClientSettings, HttpClient httpClient,
|
2020-03-24 08:47:08 +00:00
|
|
|
|
OpenIdConnectService openIdConnectService, Configuration configuration)
|
|
|
|
|
{
|
2020-03-25 20:32:28 +00:00
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_httpClientSettings = httpClientSettings;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_openIdConnectService = openIdConnectService;
|
|
|
|
|
|
|
|
|
|
_httpClient.BaseAddress = new Uri(configuration.Endpoint);
|
|
|
|
|
_httpClient.DefaultRequestHeaders.Add("Accept", MediaTypeNames.Application.Json);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_httpClientSettings.BearerToken))
|
|
|
|
|
{
|
|
|
|
|
_httpClient.DefaultRequestHeaders.Authorization =
|
|
|
|
|
new AuthenticationHeaderValue(OidcConstants.AuthenticationSchemes.AuthorizationHeaderBearer,
|
|
|
|
|
_httpClientSettings.BearerToken);
|
|
|
|
|
}
|
2020-03-24 08:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AuthenticateAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_httpClient.DefaultRequestHeaders.Authorization != null &&
|
|
|
|
|
_httpClient.DefaultRequestHeaders.Authorization.Scheme !=
|
2020-03-24 22:26:02 +00:00
|
|
|
|
OidcConstants.AuthenticationSchemes.AuthorizationHeaderBearer)
|
2020-03-24 08:47:08 +00:00
|
|
|
|
throw new AuthenticationException("Already seems to be authenticated");
|
|
|
|
|
|
|
|
|
|
var disco = await _openIdConnectService.GetDiscoveryDocumentAsync();
|
|
|
|
|
var token = await _openIdConnectService.GetTokenClientCredentialsAsync(disco.TokenEndpoint,
|
|
|
|
|
_configuration.ClientId, _configuration.ClientSecret);
|
|
|
|
|
|
|
|
|
|
if (token.IsError)
|
|
|
|
|
throw new AuthenticationException(token.Error);
|
|
|
|
|
|
2020-03-25 20:32:28 +00:00
|
|
|
|
_httpClientSettings.BearerToken = token.AccessToken;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
_httpClient.DefaultRequestHeaders.Authorization =
|
2020-03-24 22:26:02 +00:00
|
|
|
|
new AuthenticationHeaderValue(OidcConstants.AuthenticationSchemes.AuthorizationHeaderBearer,
|
2020-03-24 08:47:08 +00:00
|
|
|
|
token.AccessToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetCurrentUserCodeAsync()
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.CURRENTUSER_RESOURCE}";
|
|
|
|
|
var response = await _httpClient.GetAsync(url);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(jsonString);
|
|
|
|
|
return json["code"].Value<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
public async Task<List<UserRoot>> GetCurrentUserRootsAsync()
|
2020-03-24 08:47:08 +00:00
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.MYROOTS_RESOURCE}";
|
|
|
|
|
var response = await _httpClient.GetAsync(url);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<List<UserRoot>>(jsonString);
|
|
|
|
|
}
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
public async Task<Item> GetItemAsync(string itemCode, string itemType = null, JObject dataFilter = null)
|
|
|
|
|
{
|
|
|
|
|
if (dataFilter == null)
|
|
|
|
|
return await GetItemSingleAsync(itemCode, itemType);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
var items = await GetItemsAsync(itemCode, itemType, dataFilter);
|
|
|
|
|
return items[0];
|
|
|
|
|
}
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
public async Task<List<Item>> GetItemsAsync(string itemCode, string itemType = null, JObject dataFilter = null)
|
2020-03-24 08:47:08 +00:00
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
|
|
|
|
var queryString = HttpUtility.ParseQueryString(string.Empty);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
if (itemType != null)
|
2020-03-24 22:26:02 +00:00
|
|
|
|
queryString["it"] = itemType;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
if (dataFilter != null)
|
|
|
|
|
queryString["df"] = dataFilter.ToString(Formatting.None);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
|
|
|
|
resourceUri = (queryString.Count > 0 ? $"{resourceUri}?" : resourceUri) + queryString;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
2020-03-24 22:26:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
|
return null;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<List<Item>>(jsonString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Item> GetItemSingleAsync(string itemCode, string itemType = null)
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(itemType))
|
|
|
|
|
resourceUri = $"{resourceUri}/{itemType}";
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
|
return null;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
}
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<Item>(jsonString);
|
|
|
|
|
}
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
public async Task<List<Item>> GetItemChildrenAsync(string itemCode, string itemType = null,
|
|
|
|
|
JObject dataFilter = null)
|
2020-03-24 08:47:08 +00:00
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_CHILDREN_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
var queryString = HttpUtility.ParseQueryString(string.Empty);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
if (itemType != null)
|
2020-03-24 22:26:02 +00:00
|
|
|
|
queryString["it"] = itemType;
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
if (dataFilter != null)
|
|
|
|
|
queryString["df"] = dataFilter.ToString(Formatting.None);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
|
|
|
|
resourceUri = (queryString.Count > 0 ? $"{resourceUri}?" : resourceUri) + queryString;
|
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
2020-03-24 22:26:02 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<List<Item>>(jsonString);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Item> CreateItemAsync(ItemRequest itemRequest)
|
|
|
|
|
{
|
|
|
|
|
var jsonString = JsonConvert.SerializeObject(itemRequest);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_CREATE_RESOURCE}";
|
|
|
|
|
var content = new StringContent(jsonString, Encoding.UTF8, MediaTypeNames.Application.Json);
|
|
|
|
|
var response = await _httpClient.PostAsync(url,
|
2020-03-24 22:26:02 +00:00
|
|
|
|
content);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var jsonStringResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<Item>(jsonStringResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteItemAsync(string itemCode)
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.DeleteAsync(resourceUri);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
}
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
public async Task DeleteItemsAsync(IList<string> itemCodes)
|
|
|
|
|
{
|
|
|
|
|
var jsonString = JsonConvert.SerializeObject(itemCodes);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var content = new StringContent(jsonString);
|
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_DELETE_RESOURCE}";
|
|
|
|
|
var response = await _httpClient.PostAsync(url, content);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DownloadItemAsync(string itemCode, string filePath)
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMS_DOWNLOAD_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri, HttpCompletionOption.ResponseHeadersRead);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
2020-03-24 08:47:08 +00:00
|
|
|
|
{
|
|
|
|
|
await using Stream streamToReadFrom = await response.Content.ReadAsStreamAsync();
|
|
|
|
|
await using Stream streamToWriteTo = File.Open(filePath, FileMode.Create);
|
|
|
|
|
await streamToReadFrom.CopyToAsync(streamToWriteTo);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
throw new FileNotFoundException(response.ReasonPhrase);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> QueueTaskAsync(string itemCode, TaskRequest taskRequest)
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMTASK_REQUEST_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
var jsonString = JsonConvert.SerializeObject(taskRequest);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var content = new StringContent(jsonString, Encoding.UTF8, MediaTypeNames.Application.Json);
|
2020-03-24 22:26:02 +00:00
|
|
|
|
var response = await _httpClient.PostAsync(resourceUri, content);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var jsonStringResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(jsonStringResponse);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
return json["code"].Value<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 22:26:02 +00:00
|
|
|
|
public async Task<List<ItemTaskStatus>> GetTasksStatusAsync(string itemCode)
|
2020-03-24 08:47:08 +00:00
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMTASKS_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<List<ItemTaskStatus>>(jsonString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ItemTaskStatus> GetTaskStatusAsync(string itemCode, string itemTaskCode)
|
|
|
|
|
{
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var url = $"{_configuration.BasePath}/{ResourceEndpoints.ITEMTASK_RESOURCE}";
|
|
|
|
|
var resourceUri = string.Format(url, itemCode, itemTaskCode);
|
2020-03-24 08:47:08 +00:00
|
|
|
|
var response = await _httpClient.GetAsync(resourceUri);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
|
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<ItemTaskStatus>(jsonString);
|
|
|
|
|
}
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Experimental
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
|
/// <param name="parentItemCode"></param>
|
2020-03-25 20:32:28 +00:00
|
|
|
|
/// <param name="progressCallback"></param>
|
2020-03-25 16:31:42 +00:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="FileNotFoundException"></exception>
|
2020-03-25 20:32:28 +00:00
|
|
|
|
public async Task<UploadResults> UploadFile(string filePath, string parentItemCode,
|
|
|
|
|
Action<IUploadProgress> progressCallback = null)
|
2020-03-25 16:31:42 +00:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
throw new FileNotFoundException($"File not found {filePath}");
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var mimeTypes = new MimeTypes();
|
|
|
|
|
var mimeType = mimeTypes.GetMimeTypeFromFile(filePath);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
2020-06-15 18:50:43 +00:00
|
|
|
|
var mimeString = mimeType?.ToString() ?? "application/json";
|
|
|
|
|
|
2020-03-25 16:31:42 +00:00
|
|
|
|
await using var uploadStream = new FileStream(filePath, FileMode.OpenOrCreate);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var request = new FileRequest()
|
|
|
|
|
{
|
|
|
|
|
Name = Path.GetFileName(filePath),
|
|
|
|
|
ParentCode = parentItemCode,
|
|
|
|
|
Size = uploadStream.Length
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using var httpClient = CreateConfigurableHttpClient(_httpClient);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
var farmmapsUploader = new FarmmapsUploader(httpClient, uploadStream, request,
|
2020-06-15 18:50:43 +00:00
|
|
|
|
mimeString, ResourceEndpoints.ITEMS_UPLOAD_RESOURCE);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
2020-03-25 20:32:28 +00:00
|
|
|
|
Uri location = null;
|
|
|
|
|
farmmapsUploader.ProgressChanged += progressCallback;
|
|
|
|
|
farmmapsUploader.UploadSessionData += data => location = data.UploadUri;
|
|
|
|
|
|
|
|
|
|
var progress = await farmmapsUploader.UploadAsync();
|
|
|
|
|
|
|
|
|
|
return new UploadResults(progress, location);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Experimental
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
|
/// <param name="location"></param>
|
|
|
|
|
/// <param name="uploadCallback"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="FileNotFoundException"></exception>
|
|
|
|
|
private async Task<Uri> ResumeUploadFile(string filePath, Uri location, Action<IUploadProgress> uploadCallback)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
throw new FileNotFoundException($"File not found {filePath}");
|
|
|
|
|
|
|
|
|
|
await using var uploadStream = new FileStream(filePath, FileMode.OpenOrCreate);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
2020-03-25 16:31:42 +00:00
|
|
|
|
var request = new FileRequest()
|
|
|
|
|
{
|
|
|
|
|
Name = Path.GetFileName(filePath),
|
|
|
|
|
ParentCode = string.Empty,
|
|
|
|
|
Size = uploadStream.Length
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using var httpClient = CreateConfigurableHttpClient(_httpClient);
|
2020-03-25 20:32:28 +00:00
|
|
|
|
var farmmapsUploader = new FarmmapsUploader(httpClient, uploadStream,
|
|
|
|
|
request, string.Empty, ResourceEndpoints.ITEMS_UPLOAD_RESOURCE);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
farmmapsUploader.ProgressChanged += uploadCallback;
|
|
|
|
|
|
|
|
|
|
await farmmapsUploader.ResumeAsync(location);
|
|
|
|
|
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ConfigurableHttpClient CreateConfigurableHttpClient(HttpClient parent)
|
|
|
|
|
{
|
2020-03-25 20:32:28 +00:00
|
|
|
|
var googleHttpClient = new HttpClientFactory().CreateHttpClient(new CreateHttpClientArgs
|
|
|
|
|
{GZipEnabled = true, ApplicationName = "FarmMaps"});
|
2020-03-25 16:31:42 +00:00
|
|
|
|
googleHttpClient.BaseAddress = parent.BaseAddress;
|
|
|
|
|
googleHttpClient.DefaultRequestHeaders.Add("Accept", MediaTypeNames.Application.Json);
|
|
|
|
|
|
|
|
|
|
var authHeader = parent.DefaultRequestHeaders.Authorization;
|
2020-03-25 20:32:28 +00:00
|
|
|
|
googleHttpClient.DefaultRequestHeaders.Authorization =
|
|
|
|
|
new AuthenticationHeaderValue(authHeader.Scheme, authHeader.Parameter);
|
2020-03-25 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
return googleHttpClient;
|
|
|
|
|
}
|
2020-03-24 08:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|