/// <summary> /// The GetProfile method /// </summary> public bool GetContactList(String OAuth2Token, String MxitUserID, out MxitModel.ContactList userContactList) { userContactList = new MxitModel.ContactList(); System.Net.HttpStatusCode responseCode; logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); bool success = false; responseCode = System.Net.HttpStatusCode.Unauthorized;//Need to improve this try { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestClient..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestClient..."); var client = new RestClient(); client.BaseUrl = "http://api.mxit.com"; client.Authenticator = new RESTMxitOAuth2Authenticator(OAuth2Token); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestRequest..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestRequest..."); var RESTRequest = new RestRequest(); RESTRequest.Method = Method.GET; RESTRequest.RequestFormat = DataFormat.Json; RESTRequest.AddHeader("Content-Type", "application/json"); RESTRequest.AddHeader("Accept", "application/json"); RESTRequest.Resource = "/user/socialgraph/contactlist?filter=@Friends"; //Resource points to the method of the API we want to access logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Executing RESTRequest (ContactList)"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Executing RESTRequest (ContactList)"); RestResponse RESTResponse = (RestResponse)client.Execute(RESTRequest); //Set the out parameter, so that the calling method can redo auth if needed and retry: System.Net.HttpStatusCode RESTResponseHTTPStatusCode = RESTResponse.StatusCode; bool responseOK = (RESTResponseHTTPStatusCode == System.Net.HttpStatusCode.OK); if (responseOK) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Get ContactList OK."); //convert the rest response into a profile userContactList = JsonConvert.DeserializeObject<MxitModel.ContactList>(RESTResponse.Content); if (logger.IsDebugEnabled) Console.WriteLine(userContactList.ToString()); success = true; } else // Something went wrong, we'll handle the error code in the calling wrapper method { logger.Error(MethodBase.GetCurrentMethod().Name + "() - GetContactList Failed: (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ") Reason: " + RESTResponse.Content); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " GetContactList FAILED. (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ") Reason: " + RESTResponse.Content); responseCode = RESTResponse.StatusCode; success = false; } } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString() + " Exception GetContactList:" + ex.ToString()); logger.Error(MethodBase.GetCurrentMethod().Name + "() - Exception GetContactList: " + ex.GetType() + " " + ex.ToString()); success = false; } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); return success; }
/// <summary> /// The GetProfile method /// </summary> public bool GetContactList(String OAuth2Token, String MxitUserID, out MxitModel.ContactList userContactList) { userContactList = new MxitModel.ContactList(); MxitModel.ContactList partialContactList = new MxitModel.ContactList(); System.Net.HttpStatusCode responseCode; logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); bool success = false; responseCode = System.Net.HttpStatusCode.Unauthorized;//Need to improve this try { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestClient..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestClient..."); var client = new RestClient(); client.BaseUrl = "http://api.mxit.com"; client.Authenticator = new RESTMxitOAuth2Authenticator(OAuth2Token); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestRequest..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestRequest..."); int count = 26; //how many contacts to retrieve at a time, max is 26 int skip = 0; const int MAX_ITERATIONS = 30; //MAX_ITERATIONS * count will give the max number of contacts that can be retrieved var RESTRequest = new RestRequest(); RESTRequest.Method = Method.GET; RESTRequest.RequestFormat = DataFormat.Json; RESTRequest.AddHeader("Content-Type", "application/json"); RESTRequest.AddHeader("Accept", "application/json"); do { RESTRequest.Resource = "/user/socialgraph/contactlist?filter=@Friends&skip=" + (count*skip) + "&count=" + count; //Resource points to the method of the API we want to access partialContactList = new MxitModel.ContactList(); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Executing RESTRequest (ContactList)"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Executing RESTRequest (ContactList)"); RestResponse RESTResponse = (RestResponse)client.Execute(RESTRequest); //Set the out parameter, so that the calling method can redo auth if needed and retry: System.Net.HttpStatusCode RESTResponseHTTPStatusCode = RESTResponse.StatusCode; bool responseOK = (RESTResponseHTTPStatusCode == System.Net.HttpStatusCode.OK); if (responseOK) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Get ContactList OK."); //convert the rest response into a list of contacts partialContactList = JsonConvert.DeserializeObject<MxitModel.ContactList>(RESTResponse.Content); if (logger.IsDebugEnabled) Console.WriteLine(userContactList.ToString()); success = true; } else // Something went wrong, we'll handle the error code in the calling wrapper method { logger.Error(MethodBase.GetCurrentMethod().Name + "() - GetContactList Failed: (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ") Reason: " + RESTResponse.Content); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " GetContactList FAILED. (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ") Reason: " + RESTResponse.Content); responseCode = RESTResponse.StatusCode; success = false; } //merge the partial array with the full one userContactList.Contacts = userContactList.Contacts.Concat(partialContactList.Contacts).ToArray(); skip++; //continue while there are still results, no errors occur, or we have looped less times than the limit specified } while ((partialContactList.getTotalFriendCount() > 0) && (skip <= MAX_ITERATIONS) && (success == true)); } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString() + " Exception GetContactList:" + ex.ToString()); logger.Error(MethodBase.GetCurrentMethod().Name + "() - Exception GetContactList: " + ex.GetType() + " " + ex.ToString()); success = false; } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); return success; }