Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit 38d6d8f

Browse files
authored
Merge pull request #6 from microsoftgraph/vidadhee/BatchRequest
Add Usage documents for Batching
2 parents 712dfb3 + 5dd15cd commit 38d6d8f

File tree

2 files changed

+219
-1
lines changed

2 files changed

+219
-1
lines changed

Docs/Content/Batching.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# [Batching](https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching)
2+
3+
Batching is a way of combining multiple requests to resources in same/different workloads in a single HTTP request. This can be achieved by making a post call with those requests as a JSON payload to $batch endpoint.
4+
5+
### MSBatchRequestStep
6+
7+
To make batch request, native request objects needs to be clubbed with some `id` and `dependsOn[optional]` values. This class provides a mechanism to build such an object.
8+
9+
### MSBatchRequestContent
10+
11+
A component which eases the way of creating batch request payload. This class handles all the batch specific payload construction and stuffs, we just need to worry about individual requests.
12+
13+
14+
15+
### MSBatchResponseContent
16+
17+
A component to simplify the processing of batch responses by providing functionalities like getResponses, getResponseById.
18+
19+
20+
21+
**Update the profile photo and download the uploaded photo with batching - Making serial requests in which all individual requests depend on the previous individual request.**
22+
23+
```Objective-C
24+
//Create a native request object
25+
NSMutableURLRequest *photoUpdateUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/photo/$value"]];
26+
[photoUpdateUrlRequest setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
27+
[photoUpdateUrlRequest setHTTPMethod:@"PUT"];
28+
NSURL *fileURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"dummy" ofType:@".jpeg"]];
29+
NSData *data = [NSData dataWithContentsOfFile:[fileURL absoluteString] options:kNilOptions error:nil];
30+
[photoUpdateUrlRequest setHTTPBody:data];
31+
32+
//Create a batch step from that
33+
MSBatchRequestStep *photoUpdateStep = [[MSBatchRequestStep alloc] initWithId:@"1" request:photoUpdateUrlRequest andDependsOn:nil];
34+
35+
//Create another native request object
36+
NSMutableURLRequest *photoDownloadUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/photo/$value"]];
37+
38+
//Create a batch step from that
39+
MSBatchRequestStep *photoDownloadStep = [[MSBatchRequestStep alloc] initWithId:@"2" request:photoDownloadUrlRequest andDependsOn:@[@"1"]];
40+
41+
//Create batch request content using two batch request steps that we just created
42+
NSError *batchContentError;
43+
MSBatchRequestContent *batchRequestContent = [[MSBatchRequestContent alloc] initWithRequests:@[photoUpdateStep,photoDownloadStep] error:&batchContentError];
44+
if(batchContentError) {
45+
NSLog(@"Got error %@",batchContentError.localizedDescription);
46+
return;
47+
}
48+
49+
//Get batch content
50+
NSMutableDictionary *batchContentDictionary = [batchRequestContent getBatchRequestContent];
51+
52+
//Create native batch request
53+
NSMutableURLRequest *batchRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/$batch"]]];
54+
[batchRequest setHTTPMethod:@"POST"];
55+
[batchRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"
56+
];
57+
58+
//Add batch content as request body
59+
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:batchContentDictionary options:kNilOptions error:nil];
60+
[batchRequest setHTTPBody:bodyData];
61+
62+
//Use http client from core library to create a data task and execute
63+
MSURLSessionDataTask *dataTask = [httpClient dataTaskWithRequest:batchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
64+
if(!error){
65+
//Create batch response content using raw data
66+
MSBatchResponseContent *responseContent =[[MSBatchResponseContent alloc] initWithBatchResponseData:data options:kNilOptions error:nil];
67+
68+
//Get a response for individial request by passing in the request id.
69+
NSDictionary *responseDict = [responseContent getResponseById:@"1"];
70+
NSLog(@"%@",responseDict);
71+
72+
//Get all responses
73+
NSLog(@"%@",[responseContent getResponses]);
74+
}
75+
}];
76+
[dataTask execute];
77+
```
78+
79+
80+
81+
**GET content from `/me` and `/me/drive` endpoints - Making parallel requests in which no individual request states a dependency in the `dependsOn` property.**
82+
83+
84+
```Objective-C
85+
86+
//Create a native request object
87+
NSMutableURLRequest *meUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me"]];
88+
89+
//Create a batch step from that
90+
MSBatchRequestStep *meStep = [[MSBatchRequestStep alloc] initWithId:@"1" request:meUrlRequest andDependsOn:nil];
91+
92+
//Create another native request object
93+
NSMutableURLRequest *driveUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/drive"]];
94+
95+
//Create a batch step from that
96+
MSBatchRequestStep *driveStep = [[MSBatchRequestStep alloc] initWithId:@"2" request:driveUrlRequest andDependsOn:nil];
97+
98+
99+
//Create batch request content using two batch request steps that we just created
100+
NSError *batchContentError;
101+
MSBatchRequestContent *batchRequestContent = [[MSBatchRequestContent alloc] initWithRequests:@[meStep,driveStep] error:&batchContentError];
102+
if(batchContentError) {
103+
NSLog(@"Got error %@",batchContentError.localizedDescription);
104+
return;
105+
}
106+
107+
//Get batch content
108+
NSMutableDictionary *batchContentDictionary = [batchRequestContent getBatchRequestContent];
109+
110+
//Create native batch request
111+
NSMutableURLRequest *batchRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/$batch"]]];
112+
[batchRequest setHTTPMethod:@"POST"];
113+
[batchRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"
114+
];
115+
116+
//Add batch content as request body
117+
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:batchContentDictionary options:kNilOptions error:nil];
118+
[batchRequest setHTTPBody:bodyData];
119+
120+
//Use http client from core library to create a data task and execute
121+
MSURLSessionDataTask *dataTask = [httpClient dataTaskWithRequest:batchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
122+
123+
if(!error){
124+
NSLog(@"got batch response success");
125+
//Create batch response content using raw data
126+
MSBatchResponseContent *responseContent =[[MSBatchResponseContent alloc] initWithBatchResponseData:data options:kNilOptions error:nil];
127+
128+
//Get a response for individial request by passing in the request id.
129+
NSDictionary *responseDict = [responseContent getResponseById:@"1"];
130+
NSLog(@"%@",responseDict);
131+
132+
//Get all responses
133+
NSLog(@"%@",[responseContent getResponses]);
134+
}else{
135+
NSLog(@"got batch response Failure %@",error.localizedDescription);
136+
}
137+
}];
138+
[dataTask execute];
139+
140+
```
141+
142+
143+
144+
**Updating the photo, then downloading the updated photo and getting the details of it - Making batch request with all other request depend on one request**
145+
146+
147+
148+
```Objective-C
149+
150+
//Create a native request object
151+
NSMutableURLRequest *photoUpdateUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/photo/$value"]];
152+
[photoUpdateUrlRequest setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
153+
[photoUpdateUrlRequest setHTTPMethod:@"PUT"];
154+
NSURL *fileURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"dummy" ofType:@".jpeg"]];
155+
NSData *data = [NSData dataWithContentsOfFile:[fileURL absoluteString] options:kNilOptions error:nil];
156+
[photoUpdateUrlRequest setHTTPBody:data];
157+
158+
//Create a batch step from that
159+
MSBatchRequestStep *photoUpdateStep = [[MSBatchRequestStep alloc] initWithId:@"1" request:photoUpdateUrlRequest andDependsOn:nil];
160+
161+
//Create another native request object dependent on previous step
162+
NSMutableURLRequest *photoDownloadUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/photo/$value"]];
163+
164+
//Create a batch step from that
165+
MSBatchRequestStep *photoDownloadStep = [[MSBatchRequestStep alloc] initWithId:@"2" request:photoDownloadUrlRequest andDependsOn:@[@"1"]];
166+
167+
//Create another native request object dependent on first step
168+
NSMutableURLRequest *photoDetailsUrlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/me/photo"]];
169+
170+
//Create a batch step from that
171+
MSBatchRequestStep *photoDetailsStep = [[MSBatchRequestStep alloc] initWithId:@"3" request:photoDetailsUrlRequest andDependsOn:@[@"1"]];
172+
//Create batch request content using two batch request steps that we just created
173+
NSError *batchContentError;
174+
MSBatchRequestContent *batchRequestContent = [[MSBatchRequestContent alloc] initWithRequests:@[photoUpdateStep,photoDownloadStep,photoDetailsStep] error:&batchContentError];
175+
if(batchContentError) {
176+
NSLog(@"Got error %@",batchContentError.localizedDescription);
177+
return;
178+
}
179+
180+
//Get batch content
181+
NSMutableDictionary *batchContentDictionary = [batchRequestContent getBatchRequestContent];
182+
183+
//Create native batch request
184+
NSMutableURLRequest *batchRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/$batch"]]];
185+
[batchRequest setHTTPMethod:@"POST"];
186+
[batchRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"
187+
];
188+
189+
//Add batch content as request body
190+
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:batchContentDictionary options:kNilOptions error:nil];
191+
[batchRequest setHTTPBody:bodyData];
192+
193+
//Use http client from core library to create a data task and execute
194+
MSURLSessionDataTask *dataTask = [httpClient dataTaskWithRequest:batchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
195+
if(!error){
196+
//Create batch response content using raw data
197+
MSBatchResponseContent *responseContent =[[MSBatchResponseContent alloc] initWithBatchResponseData:data options:kNilOptions error:nil];
198+
199+
//Get a response for individial request by passing in the request id.
200+
NSDictionary *responseDict = [responseContent getResponseById:@"1"];
201+
NSLog(@"%@",responseDict);
202+
203+
//Get all responses
204+
NSLog(@"%@",[responseContent getResponses]);
205+
}
206+
}];
207+
[dataTask execute];
208+
```
209+
210+
211+
212+
### Constraints
213+
214+
Refer these [JSON Batching](https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching), [Known Issues](https://developer.microsoft.com/en-us/graph/docs/concepts/known_issues#json-batching) documents for current constraints in the batching.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,8 @@ completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
154154

155155

156156

157-
And that's it. You have now successfully made call to graph server asking information about the user
157+
And that's it. You have now successfully made call to graph server asking information about the user.
158+
159+
## Usage Resources
160+
161+
* [Batching](/docs/content/Batching.md)

0 commit comments

Comments
 (0)