Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 3df57af

Browse files
committed
adding correlationId and timestamp fields for easy debugging/investigations. Also refactored the code to reduce painpoints in debugging due to redundent code spread across codebase
1 parent 04babe0 commit 3df57af

File tree

11 files changed

+143
-156
lines changed

11 files changed

+143
-156
lines changed

OneNoteServiceSamplesWinUniversal.Shared/DataModel/ItemPageModel.cs

+53-30
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ namespace OneNoteServiceSamplesWinUniversal.DataModel
1111
{
1212
public class ItemPageModel : INotifyPropertyChanged
1313
{
14-
private string _authUserName = null; // Logged in user name
15-
private SampleDataItem _item = null; // Info about the current item
16-
private object _apiResponse = null; // Response from an API request - Either a ApiBaseResponse or a List<ApiBaseResponse>
17-
private ApiBaseResponse _selectedResponse = null; // Holds a reference to the currently selected response
14+
private string _authUserName = null; // Logged in user name
15+
private SampleDataItem _item = null; // Info about the current item
16+
private HubContext _userData = null;
17+
private object _apiResponse = null;
18+
// Response from an API request - Either a ApiBaseResponse or a List<ApiBaseResponse>
1819

19-
public event PropertyChangedEventHandler PropertyChanged; // Implements INotifyPropertyChanged for notifying the binding engine when an attribute changes
20+
private ApiBaseResponse _selectedResponse = null; // Holds a reference to the currently selected response
21+
22+
public event PropertyChangedEventHandler PropertyChanged;
23+
// Implements INotifyPropertyChanged for notifying the binding engine when an attribute changes
2024

2125
/// <summary>
2226
// NotifyPropertyChanged will fire the PropertyChanged event,
@@ -32,10 +36,7 @@ public void NotifyPropertyChanged(string propertyName)
3236

3337
public string AuthUserName
3438
{
35-
get
36-
{
37-
return _authUserName;
38-
}
39+
get { return _authUserName; }
3940

4041
set
4142
{
@@ -62,10 +63,7 @@ public string SignInButtonTitle
6263

6364
public SampleDataItem Item
6465
{
65-
get
66-
{
67-
return _item;
68-
}
66+
get { return _item; }
6967

7068
set
7169
{
@@ -85,14 +83,18 @@ public object ApiResponse
8583

8684
if (_apiResponse != null)
8785
{
88-
if (_apiResponse is ApiBaseResponse)
86+
var response = _apiResponse as ApiBaseResponse;
87+
if (response != null)
8988
{
90-
selectedResponse = _apiResponse as ApiBaseResponse;
89+
selectedResponse = response;
9190
}
9291
else
9392
{
94-
List<ApiBaseResponse> multipleResponses = _apiResponse as List<ApiBaseResponse>;
95-
selectedResponse = multipleResponses[0];
93+
var multipleResponses = _apiResponse as List<ApiBaseResponse>;
94+
if (multipleResponses != null)
95+
{
96+
selectedResponse = multipleResponses[0];
97+
}
9698
}
9799
}
98100

@@ -105,15 +107,13 @@ public object ApiResponse
105107
NotifyPropertyChanged("IsClientUrlAvailable");
106108
NotifyPropertyChanged("ResponseList");
107109
NotifyPropertyChanged("SelectedResponse");
110+
NotifyPropertyChanged("CorrelationId");
108111
}
109112
}
110113

111114
public ApiBaseResponse SelectedResponse
112115
{
113-
get
114-
{
115-
return _selectedResponse;
116-
}
116+
get { return _selectedResponse; }
117117

118118
set
119119
{
@@ -123,6 +123,17 @@ public ApiBaseResponse SelectedResponse
123123
}
124124
}
125125

126+
public HubContext UserData
127+
{
128+
get { return _userData; }
129+
130+
set
131+
{
132+
_userData = value;
133+
NotifyPropertyChanged("TimeStamp");
134+
}
135+
}
136+
126137
public string StatusCode
127138
{
128139
get
@@ -135,7 +146,7 @@ public string StatusCode
135146

136147
if (statusCode != 0)
137148
{
138-
statusCodeString = string.Format("{0}-{1}", (int)statusCode, statusCode.ToString());
149+
statusCodeString = string.Format("{0}-{1}", (int) statusCode, statusCode.ToString());
139150
}
140151
}
141152

@@ -175,10 +186,7 @@ public Thickness BodyBorderThikness
175186

176187
public bool IsResponseAvailable
177188
{
178-
get
179-
{
180-
return _apiResponse != null;
181-
}
189+
get { return _apiResponse != null; }
182190
}
183191

184192
public Visibility IsResponseListAvailable
@@ -198,10 +206,7 @@ public Visibility IsResponseListAvailable
198206

199207
public List<ApiBaseResponse> ResponseList
200208
{
201-
get
202-
{
203-
return _apiResponse as List<ApiBaseResponse>;
204-
}
209+
get { return _apiResponse as List<ApiBaseResponse>; }
205210
}
206211

207212
public Visibility IsClientUrlAvailable
@@ -248,5 +253,23 @@ public string OneNoteWebUrl
248253
return webUrl;
249254
}
250255
}
256+
257+
public string CorrelationId
258+
{
259+
get
260+
{
261+
return _selectedResponse != null && !string.IsNullOrEmpty(_selectedResponse.CorrelationId)
262+
? _selectedResponse.CorrelationId
263+
: string.Empty;
264+
}
265+
}
266+
267+
public string TimeStamp
268+
{
269+
get
270+
{
271+
return _userData != null ? _userData.TimeStamp.ToString() : string.Empty;
272+
}
273+
}
251274
}
252275
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
8+
namespace OneNoteServiceSamplesWinUniversal.OneNoteApi
9+
{
10+
public static class HttpUtils
11+
{
12+
#region Helper methods used in the examples
13+
14+
/// <summary>
15+
/// Convert the HTTP response message into a simple structure suitable for apps to process
16+
/// </summary>
17+
/// <param name="response">The response to convert</param>
18+
/// <param name="expectedStatusCode"></param>
19+
/// <returns>A simple response</returns>
20+
public static async Task<ApiBaseResponse> TranslateResponse(HttpResponseMessage response, HttpStatusCode expectedStatusCode = HttpStatusCode.Created)
21+
{
22+
ApiBaseResponse apiBaseResponse;
23+
string body = await response.Content.ReadAsStringAsync();
24+
if (response.StatusCode == expectedStatusCode
25+
/* POST calls always return 201-Created upon success */)
26+
{
27+
apiBaseResponse = JsonConvert.DeserializeObject<GenericEntityResponse>(body);
28+
}
29+
else
30+
{
31+
apiBaseResponse = new ApiBaseResponse();
32+
}
33+
34+
// Extract the correlation id. Apps should log this if they want to collect the data to diagnose failures with Microsoft support
35+
IEnumerable<string> correlationValues;
36+
if (response.Headers.TryGetValues("X-CorrelationId", out correlationValues))
37+
{
38+
apiBaseResponse.CorrelationId = correlationValues.FirstOrDefault();
39+
}
40+
apiBaseResponse.StatusCode = response.StatusCode;
41+
apiBaseResponse.Body = body;
42+
return apiBaseResponse;
43+
}
44+
45+
#endregion
46+
47+
}
48+
}

OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/PostNotebooksExample.cs

+1-37
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
// See the Apache Version 2.0 License for specific language
1717
// governing permissions and limitations under the License.
1818
//*********************************************************
19-
20-
using Newtonsoft.Json;
21-
using System.Collections.Generic;
2219
using System.Diagnostics;
2320
using System.Linq;
2421
using System.Net;
@@ -92,43 +89,10 @@ public static async Task<ApiBaseResponse> CreateSimpleNotebook(bool debug, strin
9289

9390
HttpResponseMessage response = await client.SendAsync(createMessage);
9491

95-
return await TranslateResponse(response);
92+
return await HttpUtils.TranslateResponse(response);
9693
}
9794

9895
#endregion
9996

100-
#region Helper methods used in the examples
101-
102-
/// <summary>
103-
/// Convert the HTTP response message into a simple structure suitable for apps to process
104-
/// </summary>
105-
/// <param name="response">The response to convert</param>
106-
/// <returns>A simple response</returns>
107-
private static async Task<ApiBaseResponse> TranslateResponse(HttpResponseMessage response)
108-
{
109-
ApiBaseResponse apiBaseResponse;
110-
string body = await response.Content.ReadAsStringAsync();
111-
if (response.StatusCode == HttpStatusCode.Created
112-
/* POST calls always return 201-Created upon success */)
113-
{
114-
apiBaseResponse = JsonConvert.DeserializeObject<GenericEntityResponse>(body);
115-
}
116-
else
117-
{
118-
apiBaseResponse = new ApiBaseResponse();
119-
}
120-
121-
// Extract the correlation id. Apps should log this if they want to collect the data to diagnose failures with Microsoft support
122-
IEnumerable<string> correlationValues;
123-
if (response.Headers.TryGetValues("X-CorrelationId", out correlationValues))
124-
{
125-
apiBaseResponse.CorrelationId = correlationValues.FirstOrDefault();
126-
}
127-
apiBaseResponse.StatusCode = response.StatusCode;
128-
apiBaseResponse.Body = body;
129-
return apiBaseResponse;
130-
}
131-
132-
#endregion
13397
}
13498
}

0 commit comments

Comments
 (0)