Skip to content

Commit 9429642

Browse files
authored
Merge pull request #1794 from microsoftgraph/fixBatchErrors
Adds derived BatchRequestBuilder
2 parents 0b6fb90 + e80886a commit 9429642

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ If you are looking to build the library locally for the purposes of contributing
135135
- Run `dotnet restore` from the command line in your package directory
136136
- Run `nuget restore` and `msbuild` from CLI or run Build from Visual Studio to restore Nuget packages and build the project
137137

138+
> Due to long file names you may need to run `git config --system core.longpaths true` before cloning the repo to your system.
139+
138140
## License
139141

140142
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT [license](LICENSE.txt). See [Third Party Notices](https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/master/THIRD%20PARTY%20NOTICES) for information on the packages referenced via NuGet.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.Graph.Models.ODataErrors;
11+
using Microsoft.Kiota.Abstractions;
12+
using Microsoft.Kiota.Abstractions.Serialization;
13+
14+
namespace Microsoft.Graph.Requests;
15+
16+
public class CustomBatchRequestBuilder: Core.Requests.BatchRequestBuilder
17+
{
18+
/// <summary>
19+
/// Constructs a new BatchRequestBuilder.
20+
/// </summary>
21+
/// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
22+
public CustomBatchRequestBuilder(IRequestAdapter requestAdapter): base(requestAdapter)
23+
{
24+
_ = requestAdapter ?? throw new ArgumentNullException(nameof(requestAdapter));
25+
}
26+
27+
/// <summary>
28+
/// Sends out the <see cref="BatchRequestContent"/> using the POST method
29+
/// </summary>
30+
/// <param name="batchRequestContent">The <see cref="BatchRequestContent"/> for the request</param>
31+
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for cancelling requests</param>
32+
/// <param name="errorMappings">The error mappings for using to handle errors in batch request</param>
33+
/// <returns></returns>
34+
public new Task<BatchResponseContent> PostAsync(BatchRequestContent batchRequestContent, CancellationToken cancellationToken = default, Dictionary<string, ParsableFactory<IParsable>> errorMappings = null)
35+
{
36+
var batchErrorMappings = errorMappings ?? new Dictionary<string, ParsableFactory<IParsable>> {
37+
{"4XX", ODataError.CreateFromDiscriminatorValue},
38+
{"5XX", ODataError.CreateFromDiscriminatorValue},
39+
};
40+
41+
return base.PostAsync(batchRequestContent, cancellationToken, batchErrorMappings);
42+
}
43+
44+
/// <summary>
45+
/// Sends out the <see cref="BatchRequestContentCollection"/> using the POST method
46+
/// </summary>
47+
/// <param name="batchRequestContentCollection">The <see cref="BatchRequestContentCollection"/> for the request</param>
48+
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for cancelling requests</param>
49+
/// <param name="errorMappings">The error mappings for using to handle errors in batch request</param>
50+
/// <returns></returns>
51+
public new Task<BatchResponseContentCollection> PostAsync(BatchRequestContentCollection batchRequestContentCollection, CancellationToken cancellationToken = default, Dictionary<string, ParsableFactory<IParsable>> errorMappings = null)
52+
{
53+
var batchErrorMappings = errorMappings ?? new Dictionary<string, ParsableFactory<IParsable>> {
54+
{"4XX", ODataError.CreateFromDiscriminatorValue},
55+
{"5XX", ODataError.CreateFromDiscriminatorValue},
56+
};
57+
58+
return base.PostAsync(batchRequestContentCollection, cancellationToken, batchErrorMappings);
59+
}
60+
}

src/Microsoft.Graph/GraphServiceClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.Graph
99
using System.Collections.Generic;
1010
using System.Linq;
1111
using System.Reflection;
12+
using Microsoft.Graph.Requests;
1213
using Microsoft.Graph.Core.Requests;
1314
using Microsoft.Kiota.Abstractions.Authentication;
1415
using Microsoft.Kiota.Authentication.Azure;
@@ -89,7 +90,7 @@ public BatchRequestBuilder Batch
8990
{
9091
get
9192
{
92-
return new BatchRequestBuilder(this.RequestAdapter);
93+
return new CustomBatchRequestBuilder(this.RequestAdapter);
9394
}
9495
}
9596

0 commit comments

Comments
 (0)