/// <summary> /// Reports a RayzReply /// </summary> /// <param name="rayzReplyId"> RayzReply ID </param> public async Task <IJSonObject> ReportRayzReply(String rayzReplyId) { try { var writer = new JSonWriter(true); var reader = new JSonReader(); writer.WriteObjectBegin(); writer.WriteMember("userId", _deviceId); writer.WriteMember("rayzReplyId", rayzReplyId); writer.WriteObjectEnd(); var json = new StringContent(writer.ToString()); json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/reply/report"), json); var cr = CheckResponse(response); if (cr != null) { var creply = reader.ReadAsJSonObject(cr); return(creply); } var r = await response.Content.ReadAsStringAsync(); var reply = reader.ReadAsJSonObject(r); return(reply); } catch (Exception) { return(null); } }
private static string CheckResponse(HttpResponseMessage resp) { if (resp.StatusCode != HttpStatusCode.OK) { if (resp.StatusCode == HttpStatusCode.ServiceUnavailable) { var writer = new JSonWriter(true); writer.WriteObjectBegin(); writer.WriteMember("status", "error"); writer.WriteMember("message", ErrorMessages.ServiceUnavailable); writer.WriteObjectEnd(); return(writer.ToString()); } var writer2 = new JSonWriter(true); writer2.WriteObjectBegin(); writer2.WriteMember("status", "error"); writer2.WriteMember("message", ErrorMessages.ConnectionProblem); writer2.WriteObjectEnd(); return(writer2.ToString()); } return(null); }
/// <summary> /// Creates a new RayzReply /// </summary> /// <param name="rayzId"> Rayz ID </param> /// <param name="rayzReply"> The RayzReply message </param> /// <param name="attachments"> The RayzReply Attachments </param> public async Task <IJSonObject> NewRayzReply(String rayzId, String rayzReply, ObservableCollection <RayzItAttachment> attachments) { try { var content = new MultipartFormDataContent(); var writer = new JSonWriter(true); var reader = new JSonReader(); // PLAY PROBLEM foreach (var param in content.Headers.ContentType.Parameters.Where(param => param.Name.Equals("boundary"))) { param.Value = param.Value.Replace("\"", String.Empty); } writer.WriteObjectBegin(); writer.WriteMember("userId", _deviceId); writer.WriteMember("rayzId", rayzId); writer.WriteMember("rayzReplyMessage", rayzReply); writer.WriteObjectEnd(); var json = new StringContent(writer.ToString()); content.Add(json, "\"json\""); foreach (var a in attachments) { var fileContent = new StreamContent(new MemoryStream(a.FileBody)); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"attachment\"", FileName = "\"attachment.file\"" }; fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(a.ContType); content.Add(fileContent); } var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/reply"), content); var cr = CheckResponse(response); if (cr != null) { var creply = reader.ReadAsJSonObject(cr); return(creply); } var r = await response.Content.ReadAsStringAsync(); var reply = reader.ReadAsJSonObject(r); return(reply); } catch (Exception) { return(null); } }
public async Task <IJSonObject> CheckRayzReplies(string rayzid, IEnumerable <string> rayzsList) { try { var writer = new JSonWriter(true); var reader = new JSonReader(); writer.WriteObjectBegin(); writer.WriteMember("userId", _deviceId); writer.WriteMember("rayzId", rayzid); writer.WriteMember("rayzReplyIds"); writer.WriteArrayBegin(); //Group up the records in the collection foreach (var record in rayzsList) { writer.WriteValue(record); } writer.WriteArrayEnd(); writer.WriteObjectEnd(); var json = new StringContent(writer.ToString()); json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/reply/check"), json); var cr = CheckResponse(response); if (cr != null) { var creply = reader.ReadAsJSonObject(cr); return(creply); } var r = await response.Content.ReadAsStringAsync(); var reply = reader.ReadAsJSonObject(r); return(reply); } catch (Exception) { return(null); } }
/// <summary> /// Makes a Re-Rayz Request /// </summary> /// <param name="rayzId"> Rayz ID </param> /// <param name="latitude"> Latitude Coordinate </param> /// <param name="longitude"> Longitude Coordinate </param> /// <param name="accuracy"> Accuracy of the user's position in meters </param> /// <param name="maxDistance"> Max Rayz Distance - NOT USED </param> public async Task <IJSonObject> ReRayz(String rayzId, String latitude, String longitude, String accuracy, String maxDistance) { try { var writer = new JSonWriter(true); var reader = new JSonReader(); // Make sure that lat and long are dot separated latitude = latitude.Replace(@",", @"."); longitude = longitude.Replace(@",", @"."); writer.WriteObjectBegin(); writer.WriteMember("userId", _deviceId); writer.WriteMember("rayzId", rayzId); writer.WriteMember("latitude", latitude); writer.WriteMember("longitude", longitude); writer.WriteMember("accuracy", accuracy); writer.WriteMember("maxDistance", maxDistance); writer.WriteObjectEnd(); var json = new StringContent(writer.ToString()); json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/rerayz"), json); var cr = CheckResponse(response); if (cr != null) { var creply = reader.ReadAsJSonObject(cr); return(creply); } var r = await response.Content.ReadAsStringAsync(); var reply = reader.ReadAsJSonObject(r); return(reply); } catch (Exception) { return(null); } }
public void WriteAnObject() { writer.WriteObjectBegin(); writer.WriteObjectEnd(); Console.WriteLine(writer); Assert.AreEqual("{}", writer.ToString(), "Empty object expected!"); }