private static void json_Callback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; RequestType type = (RequestType)vars[1]; HttpWebRequest myHttpWebRequest = (HttpWebRequest)vars[0]; HttpWebResponse response = null; string data; JObject obj = null; ConvMessage convMessage; try { response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); if (string.Equals(response.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { data = decompressResponse(responseStream); } else { using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); } } obj = JObject.Parse(data); } catch (IOException ioe) { obj = null; } catch (WebException we) { obj = null; } catch (JsonException je) { obj = null; } catch (Exception e) { obj = null; } finally { if (vars[2] is postResponseFunction) { postResponseFunction finalCallbackFunction = vars[2] as postResponseFunction; finalCallbackFunction(obj); } else if (vars[2] is postUploadPhotoFunction) { postUploadPhotoFunction finalCallbackFunctionForUpload = vars[2] as postUploadPhotoFunction; convMessage = vars[3] as ConvMessage; SentChatBubble chatBubble = vars[4] as SentChatBubble; finalCallbackFunctionForUpload(obj, convMessage, chatBubble); } } }
public static void unlinkAccount(postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/unlink")) as HttpWebRequest; addToken(req); req.Method = "POST"; addToken(req); req.BeginGetResponse(json_Callback, new object[] { req, RequestType.DELETE_ACCOUNT, finalCallbackFunction }); }
public static void postUpdateInfo(postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/update")) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.POST_INFO_ON_APP_UPDATE, finalCallbackFunction }); }
public static void postForCallMe(string msisdn, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/pin-call")) as HttpWebRequest; req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.CALL_ME, msisdn, finalCallbackFunction }); }
public static void postPushNotification(string uri, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/device")) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.POST_PUSHNOTIFICATION_DATA, uri, finalCallbackFunction }); }
public static void validateNumber(string phoneNo, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/validate?digits=4")) as HttpWebRequest; req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.VALIDATE_NUMBER, phoneNo, finalCallbackFunction }); }
public static void registerAccount(string pin, string unAuthMSISDN, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account")) as HttpWebRequest; req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.Headers[HttpRequestHeader.ContentEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.REGISTER_ACCOUNT, pin, unAuthMSISDN, finalCallbackFunction }); }
public static void setProfile(JObject obj, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/profile")) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.SET_PROFILE, obj, finalCallbackFunction }); }
public static void setGroupName(string name, string grpId, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + string.Format("/group/{0}/name", grpId))) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.SET_NAME, name, finalCallbackFunction }); }
public static void invite(string phone_no, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/user/invite")) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.INVITE, phone_no, finalCallbackFunction }); }
//GET request public static void createGetRequest(string requestUrl, postResponseFunction callback, bool isRelativeUrl) { HttpWebRequest request = null; if (isRelativeUrl) { request = (HttpWebRequest)HttpWebRequest.Create(BASE + requestUrl); } else { request = (HttpWebRequest)HttpWebRequest.Create(requestUrl); } request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString(); request.BeginGetResponse(GetRequestCallback, new object[] { request, callback }); }
public static void SocialPost(JObject obj, postResponseFunction finalCallbackFunction, string socialNetowrk, bool isPost) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/connect/" + socialNetowrk)) as HttpWebRequest; addToken(req); if (isPost) { req.Method = "POST"; req.ContentType = "application/json"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.SOCIAL_POST, obj, finalCallbackFunction }); } else { req.Method = "DELETE"; req.BeginGetResponse(json_Callback, new object[] { req, RequestType.SOCIAL_DELETE, finalCallbackFunction }); } }
public static void updateProfileIcon(byte[] buffer, postResponseFunction finalCallbackFunction, string groudId) { Uri requestUri; if (String.IsNullOrEmpty(groudId)) { requestUri = new Uri(BASE + "/account/avatar"); } else { requestUri = new Uri(BASE + "/group/" + groudId + "/avatar"); } HttpWebRequest req = HttpWebRequest.Create(requestUri) as HttpWebRequest; addToken(req); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.POST_PROFILE_ICON, buffer, finalCallbackFunction }); }
static void GetRequestCallback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; HttpWebRequest request = vars[0] as HttpWebRequest; JObject jObject = null; string data = ""; byte[] fileBytes = null; if (request != null) { try { WebResponse response = request.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); if (string.Equals(response.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { data = decompressResponse(responseStream); } else { if (vars[1] is postResponseFunction) { using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); } jObject = JObject.Parse(data); } else if (vars[1] is downloadFile) { using (BinaryReader br = new BinaryReader(responseStream)) { fileBytes = br.ReadBytes((int)responseStream.Length); } } } } catch (IOException ioe) { } catch (WebException we) { } catch (JsonException je) { } catch (Exception e) { } finally { if (vars[1] is postResponseFunction) { postResponseFunction finalCallbackFunction = vars[1] as postResponseFunction; finalCallbackFunction(jObject); } else if (vars[1] is downloadFile) { downloadFile finalCallbackFunction = vars[1] as downloadFile; finalCallbackFunction(fileBytes, vars[2] as object); } } } }
private static void setParams_Callback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; JObject data = new JObject(); HttpWebRequest req = vars[0] as HttpWebRequest; Stream postStream = req.EndGetRequestStream(result); postResponseFunction finalCallbackFunction = null; RequestType type = (RequestType)vars[1]; switch (type) { #region REGISTER ACCOUNT case RequestType.REGISTER_ACCOUNT: string pin = vars[2] as string; string unAuthMSISDN = vars[3] as string; finalCallbackFunction = vars[4] as postResponseFunction; data.Add("set_cookie", "0"); data.Add("devicetype", "windows"); data[HikeConstants.DEVICE_ID] = Utils.getHashedDeviceId(); //data[HikeConstants.DEVICE_TOKEN] = Utils.getDeviceId();//for push notifications data[HikeConstants.DEVICE_VERSION] = Utils.getDeviceModel(); data[HikeConstants.APP_VERSION] = Utils.getAppVersion(); string inviteToken = ""; if (!string.IsNullOrEmpty(inviteToken)) { data[HikeConstants.INVITE_TOKEN_KEY] = inviteToken; } if (pin != null) { data.Add("msisdn", unAuthMSISDN); data.Add("pin", pin); } Compress4(data.ToString(Formatting.None), postStream); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); return; #endregion #region INVITE case RequestType.INVITE: string phoneNo = vars[2] as string; data.Add("to", phoneNo); break; #endregion #region VALIDATE NUMBER case RequestType.VALIDATE_NUMBER: string numberToValidate = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("phone_no", numberToValidate); break; #endregion #region CALL ME case RequestType.CALL_ME: string msisdn = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("msisdn", msisdn); break; #endregion #region SET NAME case RequestType.SET_NAME: string nameToSet = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("name", nameToSet); break; #endregion #region SET PROFILE case RequestType.SET_PROFILE: JObject jo = vars[2] as JObject; data = jo; finalCallbackFunction = vars[3] as postResponseFunction; break; #endregion #region POST ADDRESSBOOK case RequestType.POST_ADDRESSBOOK: Dictionary <string, List <ContactInfo> > contactListMap = vars[2] as Dictionary <string, List <ContactInfo> >; finalCallbackFunction = vars[3] as postResponseFunction; data = getJsonContactList(contactListMap); string x = data.ToString(Newtonsoft.Json.Formatting.None); Compress4(x, postStream); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); ContactUtils.ContactState = ContactUtils.ContactScanState.ADDBOOK_POSTED; return; #endregion #region SOCIAL POST case RequestType.SOCIAL_POST: data = vars[2] as JObject; finalCallbackFunction = vars[3] as postResponseFunction; break; #endregion #region SOCIAL DELETE case RequestType.SOCIAL_DELETE: finalCallbackFunction = vars[2] as postResponseFunction; break; #endregion #region UPDATE ADDRESSBOOK case RequestType.UPDATE_ADDRESSBOOK: Dictionary <string, List <ContactInfo> > contacts_to_update = vars[2] as Dictionary <string, List <ContactInfo> >; JArray ids_json = vars[3] as JArray; finalCallbackFunction = vars[4] as postResponseFunction; if (ids_json != null) { data.Add("remove", ids_json); } JObject ids_to_update = getJsonContactList(contacts_to_update); if (ids_to_update != null) { data.Add("update", ids_to_update); } break; #endregion #region DELETE ACCOUNT case RequestType.DELETE_ACCOUNT: finalCallbackFunction = vars[2] as postResponseFunction; break; #endregion #region POST PROFILE ICON case RequestType.POST_PROFILE_ICON: byte[] imageBytes = (byte[])vars[2]; finalCallbackFunction = vars[3] as postResponseFunction; postStream.Write(imageBytes, 0, imageBytes.Length); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); return; #endregion #region POST PUSH NOTIFICATION DATA case RequestType.POST_PUSHNOTIFICATION_DATA: string uri = (string)vars[2]; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("dev_token", uri); data.Add("dev_type", "windows"); break; #endregion case RequestType.POST_INFO_ON_APP_UPDATE: finalCallbackFunction = vars[2] as postResponseFunction; if (Utils.IsWP8) { data["_os"] = "win8"; } else { data["_os"] = "win7"; } data["_os_version"] = Utils.getOSVersion(); data["deviceversion"] = Utils.getDeviceModel(); data["app_version"] = Utils.getAppVersion(); data["dev_type"] = "windows"; break; #region UPLOAD FILE case RequestType.UPLOAD_FILE: byte[] dataBytes = (byte[])vars[2]; postUploadPhotoFunction finalCallbackForUploadFile = vars[3] as postUploadPhotoFunction; ConvMessage convMessage = vars[4] as ConvMessage; SentChatBubble chatBubble = vars[5] as SentChatBubble; int bufferSize = 2048; int startIndex = 0; int noOfBytesToWrite = 0; double progressValue = 0; while (startIndex < dataBytes.Length) { Thread.Sleep(5); noOfBytesToWrite = dataBytes.Length - startIndex; noOfBytesToWrite = noOfBytesToWrite < bufferSize ? noOfBytesToWrite : bufferSize; postStream.Write(dataBytes, startIndex, noOfBytesToWrite); progressValue = ((double)(startIndex + noOfBytesToWrite) / dataBytes.Length) * 100; bool updated = chatBubble.updateProgress(progressValue); if (!updated) { chatBubble.setAttachmentState(Attachment.AttachmentState.CANCELED); break; } startIndex += noOfBytesToWrite; } postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackForUploadFile, convMessage, chatBubble }); return; #endregion #region DEFAULT default: break; #endregion } using (StreamWriter sw = new StreamWriter(postStream)) { string json = data.ToString(Newtonsoft.Json.Formatting.None); sw.Write(json); } postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); }
public static void postAddressBook(Dictionary <string, List <ContactInfo> > contactListMap, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/addressbook")) as HttpWebRequest; addToken(req); req.Method = "POST"; req.ContentType = "application/json"; req.Headers["Accept-Encoding"] = "gzip"; req.Headers["Content-Encoding"] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.POST_ADDRESSBOOK, contactListMap, finalCallbackFunction }); }
public static void updateAddressBook(Dictionary <string, List <ContactInfo> > contacts_to_update_or_add, JArray ids_to_delete, postResponseFunction finalCallbackFunction) { HttpWebRequest req = HttpWebRequest.Create(new Uri(BASE + "/account/addressbook")) as HttpWebRequest; addToken(req); req.Method = "PATCH"; req.ContentType = "application/json"; req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.UPDATE_ADDRESSBOOK, contacts_to_update_or_add, ids_to_delete, finalCallbackFunction }); }