Skip to content

Commit

Permalink
Merge pull request #15 from microsoft/feature/request-config
Browse files Browse the repository at this point in the history
- aligns request options with configuration revamp
  • Loading branch information
andrueastman authored Apr 28, 2022
2 parents 4da0b52 + cac903b commit 16631f6
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 97 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.0-preview.6] - 2022-04-22

### Added

- Adds support for api surface revamp for query parameters

## [1.0.0-preview.5] - 2022-04-12

### Changed
Expand Down
51 changes: 0 additions & 51 deletions Microsoft.Kiota.Abstractions.Tests/QueryParametersBaseTests.cs

This file was deleted.

43 changes: 42 additions & 1 deletion Microsoft.Kiota.Abstractions.Tests/RequestInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void AddsAndRemovesRequestOptions()
var testRequestOption = new Mock<IRequestOption>().Object;
Assert.Empty(testRequest.RequestOptions);
// Act
testRequest.AddRequestOptions(testRequestOption);
testRequest.AddRequestOptions(new IRequestOption[] {testRequestOption});
// Assert
Assert.NotEmpty(testRequest.RequestOptions);
Assert.Equal(testRequestOption, testRequest.RequestOptions.First());
Expand All @@ -48,5 +48,46 @@ public void AddsAndRemovesRequestOptions()
testRequest.RemoveRequestOptions(testRequestOption);
Assert.Empty(testRequest.RequestOptions);
}
[Fact]
public void SetsSelectQueryParameters()
{
// Arrange as the request builders would
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
UrlTemplate = "http://localhost/me{?%24select}"
};
Action<GetQueryParameters> q = x => x.Select = new[] { "id", "displayName" };
var qParams = new GetQueryParameters();
q.Invoke(qParams);

// Act
requestInfo.AddQueryParameters(qParams);

// Assert
Assert.True(requestInfo.QueryParameters.ContainsKey("%24select"));
Assert.False(requestInfo.QueryParameters.ContainsKey("select"));
Assert.Equal("%24select",requestInfo.QueryParameters.First().Key);
}
}

/// <summary>The messages in a mailbox or folder. Read-only. Nullable.</summary>
internal class GetQueryParameters
{
/// <summary>Select properties to be returned</summary>\
[QueryParameter("%24select")]
public string[] Select { get; set; }
/// <summary>Include count of items</summary>
[QueryParameter("%24count")]
public bool? Count { get; set; }
/// <summary>Expand related entities</summary>
[QueryParameter("%24filter")]
public string Filter { get; set; }
/// <summary>Order items by property values</summary>
[QueryParameter("%24orderby")]
public string[] Orderby { get; set; }
/// <summary>Search items by search phrases</summary>
[QueryParameter("%24search")]
public string Search { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview.5</VersionSuffix>
<VersionSuffix>preview.6</VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>false</SignAssembly>
<DelaySign>false</DelaySign>
Expand All @@ -23,7 +23,7 @@
<!-- Enable this line once we go live to prevent breaking changes -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
<PackageReleaseNotes>
- [Breaking] Change target runtime to netstandard 2.0
- Adds support for api surface revamp for query parameters
</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
39 changes: 0 additions & 39 deletions src/QueryParametersBase.cs

This file was deleted.

39 changes: 35 additions & 4 deletions src/RequestInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,42 @@ public Uri URI {
/// </summary>
public IDictionary<string, object> QueryParameters { get; set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Vanity method to add the query parameters to the request query parameters dictionary.
/// </summary>
/// <param name="source">The query parameters to add.</param>
public void AddQueryParameters(object source)
{
if(source == null) return;
foreach(var property in source.GetType()
.GetProperties()
.Select(
x => (
Name: x.GetCustomAttributes(false)
.OfType<QueryParameterAttribute>()
.FirstOrDefault()?.TemplateName ?? x.Name.ToFirstCharacterLowerCase(),
Value: x.GetValue(source)
)
)
.Where(x => x.Value != null && !QueryParameters.ContainsKey(x.Name)))
{
QueryParameters.AddOrReplace(property.Name, property.Value);
}
}
/// <summary>
/// The Request Headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Adds request headers to the request.
/// </summary>
/// <param name="source">The request headers to add.</param>
public void AddHeaders(IDictionary<string, string> source)
{
if(source == null) return;
foreach(var header in source)
Headers.AddOrReplace(header.Key, header.Value);
}
/// <summary>
/// The Request Body.
/// </summary>
public Stream Content { get; set; }
Expand All @@ -91,12 +123,11 @@ public Uri URI {
/// Adds an option to the request.
/// </summary>
/// <param name="options">The option to add.</param>
public void AddRequestOptions(params IRequestOption[] options)
public void AddRequestOptions(IEnumerable<IRequestOption> options)
{
if(!(options?.Any() ?? false)) return; // it's a no-op if there are no options and this avoid having to check in the code gen.
if(options == null) return;
foreach(var option in options.Where(x => x != null))
if(!_requestOptions.TryAdd(option.GetType().FullName, option))
_requestOptions[option.GetType().FullName] = option;
_requestOptions.AddOrReplace(option.GetType().FullName, option);
}
/// <summary>
/// Removes given options from the current request.
Expand Down
24 changes: 24 additions & 0 deletions src/extensions/IDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionar
{
throw new ArgumentNullException(nameof(dictionary));
}

if(key == null)
{
throw new ArgumentNullException(nameof(key));
}

if(!dictionary.ContainsKey(key))
{
Expand All @@ -37,6 +42,25 @@ public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionar

return false;
}
/// <summary>
/// Adds or replaces the element to the <see cref="IDictionary"/> instance.
/// </summary>
/// <typeparam name="TKey"> The type of the key</typeparam>
/// <typeparam name="TValue">The type of the value</typeparam>
/// <param name="dictionary">The dictionary to add to.</param>
/// <param name="key">The key parameter.</param>
/// <param name="value">The value</param>
/// <returns>The previous value if any</returns>
public static TValue AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if(!dictionary.TryAdd(key, value))
{
var oldValue = dictionary[key];
dictionary[key] = value;
return oldValue;
}
return default(TValue);
}

}
}

0 comments on commit 16631f6

Please sign in to comment.