Skip to content

Commit

Permalink
Merge pull request #19 from microsoft/feature/raw-request-bodies
Browse files Browse the repository at this point in the history
feature/raw request bodies
  • Loading branch information
baywet authored May 11, 2022
2 parents 6c52cea + 2998731 commit 3e8ff28
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 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.7] - 2022-05-11

### Added

- Adds a method to support scalar request bodies

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

### Added
Expand Down
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.6</VersionSuffix>
<VersionSuffix>preview.7</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>
- Adds support for api surface revamp for query parameters
- Adds a method to support scalar request bodies
</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
74 changes: 70 additions & 4 deletions src/RequestInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,82 @@ public void SetStreamContent(Stream content)
/// <param name="contentType">The content type to set.</param>
/// <typeparam name="T">The model type to serialize.</typeparam>
public void SetContentFromParsable<T>(IRequestAdapter requestAdapter, string contentType, params T[] items) where T : IParsable
{
using var writer = getSerializationWriter(requestAdapter, contentType, items);
if(items.Count() == 1)
writer.WriteObjectValue(null, items[0]);
else
writer.WriteCollectionOfObjectValues(null, items);
Headers.Add(ContentTypeHeader, contentType);
Content = writer.GetSerializedContent();
}
private ISerializationWriter getSerializationWriter<T>(IRequestAdapter requestAdapter, string contentType, params T[] items)
{
if(string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));
if(requestAdapter == null) throw new ArgumentNullException(nameof(requestAdapter));
if(items == null || !items.Any()) throw new InvalidOperationException($"{nameof(items)} cannot be null or empty");

using var writer = requestAdapter.SerializationWriterFactory.GetSerializationWriter(contentType);
return requestAdapter.SerializationWriterFactory.GetSerializationWriter(contentType);
}
/// <summary>
/// Sets the request body from a scalar value with the specified content type.
/// </summary>
/// <param name="requestAdapter">The core service to get the serialization writer from.</param>
/// <param name="items">The scalar values to serialize.</param>
/// <param name="contentType">The content type to set.</param>
/// <typeparam name="T">The model type to serialize.</typeparam>
public void SetContentFromScalar<T>(IRequestAdapter requestAdapter, string contentType, params T[] items)
{
using var writer = getSerializationWriter(requestAdapter, contentType, items);
if(items.Count() == 1)
writer.WriteObjectValue(null, items[0]);
switch(items[0])
{
case string s:
writer.WriteStringValue(null, s);
break;
case bool b:
writer.WriteBoolValue(null, b);
break;
case byte b:
writer.WriteByteValue(null, b);
break;
case sbyte b:
writer.WriteSbyteValue(null, b);
break;
case int i:
writer.WriteIntValue(null, i);
break;
case float f:
writer.WriteFloatValue(null, f);
break;
case long l:
writer.WriteLongValue(null, l);
break;
case double d:
writer.WriteDoubleValue(null, d);
break;
case Guid g:
writer.WriteGuidValue(null, g);
break;
case DateTimeOffset dto:
writer.WriteDateTimeOffsetValue(null, dto);
break;
case TimeSpan timeSpan:
writer.WriteTimeSpanValue(null, timeSpan);
break;
case Date date:
writer.WriteDateValue(null, date);
break;
case Time time:
writer.WriteTimeValue(null, time);
break;
case null:
writer.WriteNullValue(null);
break;
default:
throw new InvalidOperationException($"error serialization data value with unknown type {items[0]?.GetType()}");
}
else
writer.WriteCollectionOfObjectValues(null, items);
writer.WriteCollectionOfPrimitiveValues(null, items);
Headers.Add(ContentTypeHeader, contentType);
Content = writer.GetSerializedContent();
}
Expand Down

0 comments on commit 3e8ff28

Please sign in to comment.