Пример #1
0
        internal static TokenResponse GetUserToken(RemoteRuntimeSettings settings, string userName, string password, string passportUrl = null)
        {
            if (string.IsNullOrWhiteSpace(passportUrl))
            {
                passportUrl = settings.PassportServerUrl;
            }

            passportUrl = FormatUrl(passportUrl);

            var client = new TokenClient(
                $"{passportUrl}core/connect/token",
                settings.PledgeJobClient,
                settings.PledgeJobSecret);

            return(client.RequestResourceOwnerPasswordAsync(userName, password, settings.PledgeApiScope).Result);
        }
Пример #2
0
        private static async Task <string> Upload(RemoteRuntimeSettings settings, Credential credential, ListInfo listInfo)
        {
            var idResponse = ApiHelper.GetUserToken(settings, credential.UserName, credential.Password, settings.PassportServerUrl);
            var list       = await GetList(settings.PledgeApiAddress, idResponse.AccessToken, listInfo.Id, listInfo.TenantId);

            using (var httpClient = new HttpClient {
                BaseAddress = new Uri(settings.PledgeApiAddress)
            })
            {
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
                httpClient.DefaultRequestHeaders.Add(_type, list.Type);
                httpClient.DefaultRequestHeaders.Add(_listId, listInfo.Id);
                httpClient.DefaultRequestHeaders.Add(_name, listInfo.Name);
                httpClient.DefaultRequestHeaders.Add(_description, list.Description);
                httpClient.DefaultRequestHeaders.Add(_function, list.Function);
                httpClient.DefaultRequestHeaders.Add(_separator, list.Separator);
                httpClient.DefaultRequestHeaders.Add(_tenantId, listInfo.TenantId);
                httpClient.SetBearerToken(idResponse.AccessToken);

                using (var fileStream = File.Open(listInfo.File, FileMode.Open))
                {
                    var fileName = Path.GetFileName(listInfo.File);

                    var content = new MultipartFormDataContent
                    {
                        { new StreamContent(fileStream), "\"file\"", $"\"{fileName}\"" }
                    };

                    var upload = await httpClient.PostAsync("api/externallist/upload", content);

                    return(upload.IsSuccessStatusCode
                        ? "File upload completed successfully"
                        : "List upload failed.");
                }
            }
        }