-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathBulkRequestOperation.cs
More file actions
168 lines (147 loc) · 5.28 KB
/
BulkRequestOperation.cs
File metadata and controls
168 lines (147 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.SCIM
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
[DataContract]
public sealed class BulkRequestOperation : BulkOperation
{
private Uri path;
[DataMember(Name = ProtocolAttributeNames.Path, Order = 0)]
private string pathValue;
private BulkRequestOperation()
{
}
[DataMember(Name = ProtocolAttributeNames.Data, Order = 4)]
public object Data
{
get;
set;
}
public Uri Path
{
get => this.path;
set
{
this.path = value;
this.pathValue = new SystemForCrossDomainIdentityManagementResourceIdentifier(value).RelativePath;
}
}
public static BulkRequestOperation CreateDeleteOperation(Uri resource) =>
new BulkRequestOperation
{
Method = HttpMethod.Delete,
Path = resource ?? throw new ArgumentNullException(nameof(resource))
};
public static BulkRequestOperation CreatePatchOperation(Uri resource, PatchRequest2 data)
{
if (null == resource)
{
throw new ArgumentNullException(nameof(resource));
}
if (null == data)
{
throw new ArgumentNullException(nameof(data));
}
PatchRequest2 patchRequest = new PatchRequest2(data.Operations);
BulkRequestOperation result = new BulkRequestOperation
{
Method = ProtocolExtensions.PatchMethod,
Path = resource,
Data = patchRequest
};
return result;
}
public static BulkRequestOperation CreatePostOperation(Resource data)
{
if (null == data)
{
throw new ArgumentNullException(nameof(data));
}
if (null == data.Schemas)
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementProtocolResources.ExceptionUnidentifiableSchema);
}
if (!data.Schemas.Any())
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementProtocolResources.ExceptionUnidentifiableSchema);
}
IList<Uri> paths = new List<Uri>(1);
IEnumerable<ISchemaIdentifier> schemaIdentifiers =
data
.Schemas
.Select(
(string item) =>
new SchemaIdentifier(item));
foreach (ISchemaIdentifier schemaIdentifier in schemaIdentifiers)
{
Uri schemaIdentifierPath = null;
if (schemaIdentifier.TryFindPath(out string pathValue))
{
schemaIdentifierPath = new Uri(pathValue, UriKind.Relative);
if
(
!paths
.Any(
(Uri item) =>
0 == Uri.Compare(
item,
schemaIdentifierPath,
UriComponents.AbsoluteUri,
UriFormat.UriEscaped,
StringComparison.OrdinalIgnoreCase))
)
{
paths.Add(schemaIdentifierPath);
}
}
if (data.TryGetPathIdentifier(out Uri resourcePath))
{
if
(
!paths
.Any(
(Uri item) =>
0 == Uri.Compare(
item,
resourcePath,
UriComponents.AbsoluteUri,
UriFormat.UriEscaped,
StringComparison.OrdinalIgnoreCase))
)
{
paths.Add(resourcePath);
}
}
}
if (paths.Count != 1)
{
string schemas = string.Join(Environment.NewLine, data.Schemas);
throw new NotSupportedException(schemas);
}
BulkRequestOperation result = new BulkRequestOperation
{
path = paths.Single(),
Method = HttpMethod.Post,
Data = data
};
return result;
}
private void InitializePath(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
this.path = null;
return;
}
this.path = new Uri(value, UriKind.Relative);
}
private void InitializePath() => this.InitializePath(this.pathValue);
[OnDeserialized]
private void OnDeserialized(StreamingContext _) => this.InitializePath();
}
}