public void AppendsRelativePath(string url, string relativePath, string expected) { var uri = new Uri(url, UriKind.Absolute); var expectedUri = new Uri(expected, UriKind.Absolute); var result = uri.Append(relativePath); Assert.Equal(expectedUri, result); }
public static Uri Combine(Uri baseUrl, params string[] paths) { return(baseUrl.Append(paths)); }
private async Task <CollectionActionResult> ExecuteAsync( Uri sourceUrl, ICollectionNode sourceNode, TCollection target, IReadOnlyCollection <IUntypedWriteableProperty> properties, CancellationToken cancellationToken) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Perform operation on collection {sourceUrl} with existing target {target.DestinationUrl}."); } var documentActionResults = ImmutableList <ActionResult> .Empty; var collectionActionResults = ImmutableList <CollectionActionResult> .Empty; var subNodeProperties = new Dictionary <string, IReadOnlyCollection <IUntypedWriteableProperty> >(); foreach (var childNode in sourceNode.Nodes) { var subProperties = await GetWriteableProperties(childNode.Collection, cancellationToken).ConfigureAwait(false); subNodeProperties.Add(childNode.Name, subProperties); } foreach (var document in sourceNode.Documents) { var docUrl = sourceUrl.Append(document); if (target.Created) { // Collection was created by us - we just assume that the document doesn't exist var missingTarget = target.NewMissing(document.Name); var docResult = await ExecuteAsync(docUrl, document, missingTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } else { var foundTarget = await target.GetAsync(document.Name, cancellationToken).ConfigureAwait(false); var docTarget = foundTarget as TDocument; if (docTarget != null) { // We found a document: Business as usual when we're allowed to overwrite it var docResult = await ExecuteAsync(docUrl, document, docTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } else { var collTarget = foundTarget as TCollection; if (collTarget != null) { // We found a collection instead of a document _logger.LogDebug($"{target.DestinationUrl}: Found a collection instead of a document"); var docResult = new ActionResult(ActionStatus.OverwriteFailed, foundTarget); documentActionResults = documentActionResults.Add(docResult); } else { // We didn't find anything: Business as usual var missingTarget = (TMissing)foundTarget; var docResult = await ExecuteAsync(docUrl, document, missingTarget, cancellationToken).ConfigureAwait(false); documentActionResults = documentActionResults.Add(docResult); } } } } foreach (var childNode in sourceNode.Nodes) { var childProperties = subNodeProperties[childNode.Name]; var collection = childNode.Collection; var docUrl = sourceUrl.Append(childNode.Collection); if (target.Created) { // Collection was created by us - we just assume that the sub collection doesn't exist var missingTarget = target.NewMissing(childNode.Name); var newColl = await missingTarget.CreateCollectionAsync(cancellationToken).ConfigureAwait(false); var collResult = await ExecuteAsync(docUrl, childNode, newColl, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } else { // Test if the target node exists var foundTarget = await target.GetAsync(collection.Name, cancellationToken).ConfigureAwait(false); var docTarget = foundTarget as TDocument; if (docTarget != null) { // We found a document instead of a collection _logger.LogDebug($"{target.DestinationUrl}: Found a document instead of a collection"); var collResult = new CollectionActionResult(ActionStatus.OverwriteFailed, foundTarget); collectionActionResults = collectionActionResults.Add(collResult); } else { var collTarget = foundTarget as TCollection; if (collTarget != null) { // We found a collection: Business as usual var collResult = await ExecuteAsync(docUrl, childNode, collTarget, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } else { // We didn't find anything: Business as usual var missingTarget = (TMissing)foundTarget; var newColl = await missingTarget.CreateCollectionAsync(cancellationToken).ConfigureAwait(false); var collResult = await ExecuteAsync(docUrl, childNode, newColl, childProperties, cancellationToken).ConfigureAwait(false); collectionActionResults = collectionActionResults.Add(collResult); } } } } try { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Set properties on collection {target.DestinationUrl}."); } var failedPropertyNames = await target.SetPropertiesAsync(properties, cancellationToken).ConfigureAwait(false); if (failedPropertyNames.Count != 0) { _logger.LogDebug($"{target.DestinationUrl}: Failed setting properties {string.Join(", ", failedPropertyNames.Select(x => x.ToString()))}"); return(new CollectionActionResult(ActionStatus.PropSetFailed, target) { FailedProperties = failedPropertyNames, CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); } await _handler.ExecuteAsync(sourceNode.Collection, target, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug($"{sourceNode.Collection.Path}: Cleanup failed with exception {ex.Message}"); return(new CollectionActionResult(ActionStatus.CleanupFailed, target) { Exception = ex, CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); } return(new CollectionActionResult(ActionStatus.Created, target) { CollectionActionResults = collectionActionResults, DocumentActionResults = documentActionResults, }); }
/// <summary> /// Append a relative URI to the <paramref name="baseUri"/> /// </summary> /// <param name="baseUri">The base URL to append the <paramref name="relativeUri"/> to</param> /// <param name="relativeUri">The relative URL to append to the <paramref name="baseUri"/></param> /// <returns>The <paramref name="baseUri"/> with the appended <paramref name="relativeUri"/>.</returns> public static Uri Append(this Uri baseUri, Uri relativeUri) { return(baseUri.Append(relativeUri.OriginalString, true)); }
/// <summary> /// Appends a collection name to the <paramref name="baseUri"/> /// </summary> /// <param name="baseUri">The base URL to append the name to</param> /// <param name="collectionName">The collection name to append</param> /// <returns>The <paramref name="baseUri"/> with the appended name.</returns> public static Uri AppendDirectory(this Uri baseUri, string collectionName) { return(baseUri.Append(collectionName.UriEscape() + "/", true)); }
public Task <object> QueryRoomsAsync() { return(queue.QueueWorkAsync(token => Request <object>(teamProjectCollectionUri.Append("_apis/chat/rooms?api-version=1.0")))); }
private Task <string> GetCsrfTokenAsync(Uri baseUri) => GetStringFromHtmlSequenceAsync(baseUri.Append("/login"), m_LOGINSTARTSTRING, m_HTMLINPUTENDSTRING);
public IUrlParameterBuilder SetRelativeUri(string relativeUri) { _baseUri = _baseUri.Append(relativeUri); return(this); }
private static string GetMoveVersionUri(Uri versionUri) { return(versionUri.Append("move").ToString()); }
private static string GetUnresolvedIssueCountUri(Uri versionUri) { return(versionUri.Append("unresolvedIssueCount").ToString()); }
private static string GetRelatedIssuesCountUri(Uri versionUri) { return(versionUri.Append("relatedIssuesCount").ToString()); }
/// <summary> /// Prepare paging and filter request /// </summary> /// <param name="address"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <param name="filter"></param> /// <returns></returns> private ZonkyHttpRequestMessage PreparePagingFilterRequest(string address, int page, int pageSize, FilterOptions filter = null) { return(new ZonkyHttpRequestMessage(HttpMethod.Get, _baseUrl.Append(address)) .AddFilterOptions(filter) .AddPaging(page, pageSize)); }
/// <summary> /// Retrieves detailed information about the selected role in the specified project. /// </summary> /// <param name="projectUri">The URI of the project resource.</param> /// <param name="roleId">The Id of the role.</param> /// <returns>Detailed information about the selected role.</returns> /// <exception cref="WebServiceException">The project role or role ID was not found, or the calling user does not have permission to view it.</exception> public ProjectRole GetRole(Uri projectUri, int roleId) { var uri = projectUri.Append(ProjectRoleUriPostfix).Append(roleId.ToString()); return(client.Get <ProjectRole>(uri.ToString())); }
public Task <string> Get(Uri uri, object queryObj) => Get(uri.Append(queryObj));
/// <summary> /// Returns XML /// </summary> /// <param name="uri"></param> /// <returns></returns> public XElement Get(string uri) { var body = _web.Get(_hostname.Append(uri).ToString()).Body; return(XElement.Parse(body)); }
protected Task <T> GetAsync <T>([NotNull] string query, CancellationToken?cancellationToken = null) => _HttpClient.GetFromJsonAsync <T>(_BaseUri.Append(query), cancellationToken);
/// <summary> /// https://github.com/Azure/azure-functions-host/wiki/Key-management-API#post /// </summary> /// <param name="dbUser">user name stored as PK, used for naming the keys</param> /// <returns></returns> private static async Task <string> RequestANewKeyForUser(string dbUser, TraceWriter log) { var protocol = FunctionRunsLocally ? "http://" : "https://"; var site = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"); var keyname = $"key_{dbUser}"; var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{Credentials.Username}:{Credentials.Password}")); var siteUrl = new Uri(protocol + Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME")); string JWT = null; if (!FunctionRunsLocally) { var apiUrl = new Uri($"https://{site}.scm.azurewebsites.net/api"); Uri jwtEndpoint = apiUrl.Append("functions", "admin", "token"); try { using (var httpClient = new HttpClient()) { log.Info("Request to Kudu API for a token"); httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}"); var response = await httpClient.GetAsync(jwtEndpoint); if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK) { JWT = response.Content.ReadAsStringAsync().Result.Trim('"'); } else { throw new Exception($"Status code = {response.StatusCode}, message = {response.RequestMessage}"); } } } catch (Exception ex) { log.Error($"Failed to get token at endpoint {jwtEndpoint}", ex); throw; } if (string.IsNullOrEmpty(JWT)) { log.Error("Downloaded JWT token but it's empty"); throw new Exception("Downloaded JWT token but it's empty"); } } Uri endpoint = siteUrl.Append($"/admin/host/keys/{keyname}"); try { using (var httpClient = new HttpClient()) { log.Info("Request to Key management API for a new key"); if (!FunctionRunsLocally) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWT); } var response = await httpClient.PostAsync(endpoint, null); if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK) { var responseDto = await response.Content.ReadAsDeserializedJson <ResponseForCreatingOrUpdatingTheKey>(); return(responseDto.value); } else { throw new Exception($"Status code = {response.StatusCode}, message = {response.RequestMessage}"); } } } catch (Exception ex) { log.Error($"Failed to get a new key at endpoint {endpoint}", ex); throw; } }
public Uri GetRedirectUri(Uri baseUri, bool useNewApi) => useNewApi ? default(Uri) : baseUri.Append(_device.DeviceName);
/// <summary> /// Appends the name of a document to the <paramref name="baseUri"/> /// </summary> /// <param name="baseUri">The base URL to append the <paramref name="entry"/> name to</param> /// <param name="entry">The <see cref="IDocument"/> whose name to append to the <paramref name="baseUri"/></param> /// <returns>The <paramref name="baseUri"/> with the <paramref name="entry"/> name appended.</returns> public static Uri Append(this Uri baseUri, IDocument entry) { return(baseUri.Append(entry.Name.UriEscape(), true)); }
public async Task <ClientCreationData> CreateClientAsync(string Url, string Username, string Password) { _loggingService.WriteLine("Creating a new client..."); string token = string.Empty; bool useNewApi = false; int step = 1; HttpResponseMessage message = null; try { _http = new HttpClient(); var instanceUri = new Uri(Url); _loggingService.WriteLine("Logging in to get a cookie... (mmh, cookies...)"); _loggingService.WriteLine($"URI: {instanceUri.Append("/login_check")}"); // Step 1: Login to get a cookie. var loginContent = new StringContent($"_username={System.Net.WebUtility.UrlEncode(Username)}&_password={System.Net.WebUtility.UrlEncode(Password)}&_csrf_token={await GetCsrfTokenAsync(instanceUri)}", Encoding.UTF8, "application/x-www-form-urlencoded"); var loginResponse = await _http.PostAsync(instanceUri.Append("/login_check"), loginContent); // TODO: Apparently the HttpClient doesn't handle cookies properly. Find a workaround for this issue. // In the meantime, an UWP implementation based on the Windows.Web.HttpClient is used. if (!loginResponse.IsSuccessStatusCode) { _loggingService.WriteLine($"Failed. Resulted content: {await loginResponse.Content.ReadAsStringAsync()}", LoggingCategory.Warning); return(new ClientCreationData()); } // Step 2: Get the client token _loggingService.WriteLine("Get the client token..."); step++; var clientCreateUri = instanceUri.Append("/developer/client/create"); token = await GetStringFromHtmlSequenceAsync(clientCreateUri, m_TOKENSTARTSTRING, m_HTMLINPUTENDSTRING); _loggingService.WriteLine($"URI: {clientCreateUri}"); _loggingService.WriteLine($"Token: {token}"); // Step 3: Create the new client _loggingService.WriteLine("Creating the new client..."); step++; string stringContent = string.Empty; useNewApi = (await _client.GetVersionAsync()).Minor > 0; _loggingService.WriteLine($"Use new API: {useNewApi}"); stringContent = $"client[redirect_uris]={GetRedirectUri(instanceUri, useNewApi)}&client[save]=&client[_token]={token}"; if (useNewApi) { stringContent = $"client[name]={_device.DeviceName}&" + stringContent; } _loggingService.WriteLine($"Content: {stringContent}"); var addContent = new StringContent(stringContent, Encoding.UTF8, "application/x-www-form-urlencoded"); var addResponse = _http.PostAsync(clientCreateUri, addContent); message = await addResponse; if (!message.IsSuccessStatusCode) { _loggingService.WriteLine($"Failed. Resulted content: {await message.Content.ReadAsStringAsync()}", LoggingCategory.Warning); return(new ClientCreationData()); } string content = await message.Content.ReadAsStringAsync(); _loggingService.WriteLine($"Parsing the resulted string: {content}"); var result = ParseResult(content, useNewApi) ?? new ClientCreationData(); _loggingService.WriteLineIf(result.Success, "Success!"); _http.Dispose(); return(result); } catch (Exception e) { _loggingService.TrackException(e); return(new ClientCreationData()); } }
public static Uri Append(this Uri baseUri, Uri relativeUri) => baseUri.Append(relativeUri.ToString());