private async Task GetSampleDataAsync() { Uri dataUri = new Uri("ms-appx:///DataModel/InitialData.json"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri); string jsonText = await FileIO.ReadTextAsync(file); // Ensure that we have a valid access token before updating the data string accessToken = await AuthenticationHelper.EnsureAccessTokenAvailableAsync(); lock (this.Groups) { if (this.Groups.Count != 0) { return; } JsonObject jsonObject = JsonObject.Parse(jsonText); JsonArray jsonArray = jsonObject["Groups"].GetArray(); foreach (JsonValue groupValue in jsonArray) { JsonObject groupObject = groupValue.GetObject(); DataGroup group = new DataGroup(groupObject["UniqueId"].GetString(), groupObject["Title"].GetString(), groupObject["Subtitle"].GetString(), groupObject["ImagePath"].GetString(), groupObject["MoreInfoText"].GetString(), new Uri(groupObject["MoreInfoUri"].GetString())); foreach (JsonValue itemValue in groupObject["Items"].GetArray()) { JsonObject itemObject = itemValue.GetObject(); JsonObject requestObject = itemObject["Request"].GetObject(); //Add the Authorization header with the access token. JsonObject jsonHeaders = requestObject["Headers"].GetObject(); jsonHeaders["Authorization"] = JsonValue.CreateStringValue(jsonHeaders["Authorization"].GetString() + accessToken); // The body can be a JSON object or string, we need to // determine the type of JSON value and use the right // method to get the value. string strBody; if (requestObject["Body"].ValueType == JsonValueType.Object) { strBody = requestObject["Body"].GetObject().Stringify(); } else if (requestObject["Body"].ValueType == JsonValueType.String) { strBody = requestObject["Body"].GetString(); } else { throw new NotSupportedException("The body should only be of value JSON object or JSON string."); } //Create the request object RequestItem request = new RequestItem(new Uri(requestObject["ApiUrl"].GetString(), UriKind.Relative), requestObject["Method"].GetString(), jsonHeaders, strBody); //Create the data item object DataItem item = new DataItem(itemObject["UniqueId"].GetString(), itemObject["Title"].GetString(), itemObject["Subtitle"].GetString(), itemObject["ImagePath"].GetString() ); // Add the request object to the item item.Request = request; //Add the item to the group group.Items.Add(item); } this.Groups.Add(group); } } }
private async Task GetSampleDataAsync() { Uri dataUri = new Uri("ms-appx:///DataModel/InitialData.json"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri); string jsonText = await FileIO.ReadTextAsync(file); // Ensure that we have a valid access token before updating the data string accessToken = await AuthenticationHelper.EnsureAccessTokenAvailableAsync(); lock (this.Groups) { if (this.Groups.Count != 0) return; JsonObject jsonObject = JsonObject.Parse(jsonText); JsonArray jsonArray = jsonObject["Groups"].GetArray(); foreach (JsonValue groupValue in jsonArray) { JsonObject groupObject = groupValue.GetObject(); DataGroup group = new DataGroup(groupObject["UniqueId"].GetString(), groupObject["Title"].GetString(), groupObject["Subtitle"].GetString(), groupObject["ImagePath"].GetString(), groupObject["MoreInfoText"].GetString(), new Uri(groupObject["MoreInfoUri"].GetString())); foreach (JsonValue itemValue in groupObject["Items"].GetArray()) { JsonObject itemObject = itemValue.GetObject(); JsonObject requestObject = itemObject["Request"].GetObject(); //Add the Authorization header with the access token. JsonObject jsonHeaders = requestObject["Headers"].GetObject(); jsonHeaders["Authorization"] = JsonValue.CreateStringValue(jsonHeaders["Authorization"].GetString() + accessToken); // The body can be a JSON object or string, we need to // determine the type of JSON value and use the right // method to get the value. string strBody; if (requestObject["Body"].ValueType == JsonValueType.Object) { strBody = requestObject["Body"].GetObject().Stringify(); } else if (requestObject["Body"].ValueType == JsonValueType.String) { strBody = requestObject["Body"].GetString(); } else { throw new NotSupportedException("The body should only be of value JSON object or JSON string."); } //Create the request object RequestItem request = new RequestItem(new Uri(requestObject["ApiUrl"].GetString(), UriKind.Relative), requestObject["Method"].GetString(), jsonHeaders, strBody); //Create the data item object DataItem item = new DataItem(itemObject["UniqueId"].GetString(), itemObject["Title"].GetString(), itemObject["Subtitle"].GetString(), itemObject["ImagePath"].GetString() ); // Add the request object to the item item.Request = request; //Add the item to the group group.Items.Add(item); } this.Groups.Add(group); } } }
public static async Task <ResponseItem> GetResponseAsync(RequestItem request) { HttpWebRequest endpointRequest; //Validate that the resulting URI is well-formed. Uri endpointUri = new Uri(new Uri(AuthenticationHelper.ServiceResourceId), request.ApiUrl); endpointRequest = (HttpWebRequest)HttpWebRequest.Create(endpointUri.AbsoluteUri); endpointRequest.Method = request.Method; // Add the headers to the request foreach (KeyValuePair <string, IJsonValue> header in request.Headers) { // Accept and contenttype are special cases that must be added using the Accept and ContentType properties // All other headers can be added using the Headers collection switch (header.Key.ToLower()) { case "accept": endpointRequest.Accept = header.Value.GetString(); break; case "content-type": endpointRequest.ContentType = header.Value.GetString(); break; default: endpointRequest.Headers[header.Key] = header.Value.GetString(); break; } } //Request body, added to the request only if method is POST if (request.Method == "POST") { // If the request is a create or update file operation, we use the rawBody parameter // otherwise we use the request.Body string postData = request.Body; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(postData); System.IO.Stream newStream = await endpointRequest.GetRequestStreamAsync(); newStream.Write(byte1, 0, byte1.Length); } Stream responseStream; WebHeaderCollection responseHeaders; string status; Uri responseUri; JsonObject headers = null; string body = null; string responseString = string.Empty; try { // If the request is successful we can use the endpointResponse object HttpWebResponse endpointResponse = (HttpWebResponse)await endpointRequest.GetResponseAsync(); status = (int)endpointResponse.StatusCode + " - " + endpointResponse.StatusDescription; responseStream = endpointResponse.GetResponseStream(); responseUri = endpointResponse.ResponseUri; responseHeaders = endpointResponse.Headers; } catch (WebException we) { // If the request fails, we must use the response stream from the exception status = we.Message; responseStream = we.Response.GetResponseStream(); responseUri = we.Response.ResponseUri; responseHeaders = we.Response.Headers; } using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { responseString = await reader.ReadToEndAsync(); } // Free resources used by the stream responseStream.Dispose(); if (!String.IsNullOrEmpty(responseString)) { body = responseString; } headers = new JsonObject(); for (int i = 0; i < responseHeaders.Count; i++) { string key = responseHeaders.AllKeys[i].ToString(); headers.Add(key, JsonValue.CreateStringValue(responseHeaders[key])); } return(new ResponseItem(responseUri, status, headers, body)); }
public static async Task<ResponseItem> GetResponseAsync(RequestItem request) { HttpWebRequest endpointRequest; //Validate that the resulting URI is well-formed. Uri endpointUri = new Uri(new Uri(AuthenticationHelper.ServiceResourceId), request.ApiUrl); endpointRequest = (HttpWebRequest)HttpWebRequest.Create(endpointUri.AbsoluteUri); endpointRequest.Method = request.Method; // Add the headers to the request foreach (KeyValuePair<string, IJsonValue> header in request.Headers) { // Accept and contenttype are special cases that must be added using the Accept and ContentType properties // All other headers can be added using the Headers collection switch (header.Key.ToLower()) { case "accept": endpointRequest.Accept = header.Value.GetString(); break; case "content-type": endpointRequest.ContentType = header.Value.GetString(); break; default: endpointRequest.Headers[header.Key] = header.Value.GetString(); break; } } //Request body, added to the request only if method is POST if (request.Method == "POST") { // If the request is a create or update file operation, we use the rawBody parameter // otherwise we use the request.Body string postData = request.Body; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(postData); System.IO.Stream newStream = await endpointRequest.GetRequestStreamAsync(); newStream.Write(byte1, 0, byte1.Length); } Stream responseStream; WebHeaderCollection responseHeaders; string status; Uri responseUri; JsonObject headers = null; string body = null; string responseString = string.Empty; try { // If the request is successful we can use the endpointResponse object HttpWebResponse endpointResponse = (HttpWebResponse)await endpointRequest.GetResponseAsync(); status = (int)endpointResponse.StatusCode + " - " + endpointResponse.StatusDescription; responseStream = endpointResponse.GetResponseStream(); responseUri = endpointResponse.ResponseUri; responseHeaders = endpointResponse.Headers; } catch (WebException we) { // If the request fails, we must use the response stream from the exception status = we.Message; responseStream = we.Response.GetResponseStream(); responseUri = we.Response.ResponseUri; responseHeaders = we.Response.Headers; } using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { responseString = await reader.ReadToEndAsync(); } // Free resources used by the stream responseStream.Dispose(); if (!String.IsNullOrEmpty(responseString)) { body = responseString; } headers = new JsonObject(); for (int i = 0; i < responseHeaders.Count; i++) { string key = responseHeaders.AllKeys[i].ToString(); headers.Add(key, JsonValue.CreateStringValue(responseHeaders[key])); } return new ResponseItem(responseUri, status, headers, body); }