You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
7.6 KiB

  1. using Newtonsoft.Json;
  2. using Swift.API.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Web.Script.Serialization;
  10. namespace Swift.API.TPAPIs.GMESocialWallAPI
  11. {
  12. public class SocialWallAPIService : ISocialWallAPIService
  13. {
  14. private const string providerName = "FuseMachine";
  15. string logId = "";
  16. string _baseURL = "http://10.1.1.171:8080/v1";
  17. DbResult dbResult = new DbResult();
  18. DbResult _dbResult = new DbResult();
  19. public FeedResponse GetFeeds(FeedRequest data, out DbResult err)
  20. {
  21. FeedResponse _feedResponse = new FeedResponse();
  22. string str = "";
  23. string sResponseFromServer = "";
  24. var serializer = new JavaScriptSerializer();
  25. var auth = Utility.GetSocialWallApiAuthKey();
  26. var _request = serializer.Serialize(data);
  27. _dbResult = Utility.LogRequest(data.userId, providerName, "GetFeeds", "", _request);
  28. logId = _dbResult.Id;
  29. try
  30. {
  31. HttpWebRequest _httpRequest = (HttpWebRequest)WebRequest.Create(_baseURL + "/feeds/admins?userId=" + data.userId + "&country=" + data.country + "&onlyReported=" + data.onlyReported + "&before=" + data.before + "&after="+ data.after + "&limit=" + data.limit);
  32. _httpRequest.ContentType = "application/json";
  33. _httpRequest.Method = "GET";
  34. _httpRequest.Headers.Add("Authorization", auth);
  35. var _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
  36. if (_httpResponse.StatusCode==HttpStatusCode.OK)
  37. {
  38. using (var _streamReader = new StreamReader(_httpResponse.GetResponseStream()))
  39. {
  40. sResponseFromServer = _streamReader.ReadToEnd();
  41. _feedResponse = new JavaScriptSerializer().Deserialize<FeedResponse>(sResponseFromServer);
  42. dbResult.SetError("0", "Success", "");
  43. }
  44. }
  45. else
  46. {
  47. //_dbResult.SetError("1",_httpResponse)
  48. }
  49. _httpResponse.Close();
  50. Utility.LogResponse(logId, sResponseFromServer, dbResult.ErrorCode, dbResult.Msg);
  51. }
  52. catch (Exception ex)
  53. {
  54. dbResult.SetError("1", ex.Message, "");
  55. str = serializer.Serialize(_dbResult);
  56. }
  57. err = dbResult;
  58. return _feedResponse;
  59. }
  60. public Feeds GetSpecificFeed(FeedQueryParameters data, out DbResult err)
  61. {
  62. Feeds _feed = new Feeds();
  63. string str = "";
  64. var serializer = new JavaScriptSerializer();
  65. var auth = Utility.GetSocialWallApiAuthKey();
  66. var _request = serializer.Serialize(data);
  67. _dbResult = Utility.LogRequest(data.userId, providerName, "GetFeeds", "", _request);
  68. logId = _dbResult.Id;
  69. try
  70. {
  71. HttpWebRequest _httpRequest = (HttpWebRequest)WebRequest.Create(_baseURL + "/feeds/" + data.feedId + "?userId=" + data.userId);
  72. _httpRequest.ContentType = "application/json";
  73. _httpRequest.Method = "GET";
  74. _httpRequest.Headers.Add("Authorization", auth);
  75. var _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
  76. using (var _streamReader = new StreamReader(_httpResponse.GetResponseStream()))
  77. {
  78. String sResponseFromServer = _streamReader.ReadToEnd();
  79. _feed = new JavaScriptSerializer().Deserialize<Feeds>(sResponseFromServer);
  80. dbResult.SetError("0", "Success", "");
  81. }
  82. _httpResponse.Close();
  83. var responseXml = Utility.ObjectToXML(_feed);
  84. Utility.LogResponse(logId, responseXml, "", "");
  85. }
  86. catch (WebException ex)
  87. {
  88. var res=ex.Response.ToString();
  89. dbResult.SetError("1", ex.Message, "");
  90. str = serializer.Serialize(dbResult);
  91. }
  92. err = dbResult;
  93. return _feed;
  94. }
  95. public List<ReportedFeedResponse> GetFeedReportDetails(FeedQueryParameters data, out DbResult err)
  96. {
  97. List<ReportedFeedResponse> _reportedFeedResponse = new List<ReportedFeedResponse>();
  98. string str = "";
  99. var serializer = new JavaScriptSerializer();
  100. var auth = Utility.GetSocialWallApiAuthKey();
  101. var _request = serializer.Serialize(data);
  102. _dbResult = Utility.LogRequest(data.userId, providerName, "GetFeedReportDetails", data.feedId, _request);
  103. logId = _dbResult.Id;
  104. try
  105. {
  106. HttpWebRequest _httpRequest = (HttpWebRequest)WebRequest.Create(_baseURL + "/feeds/" + data.feedId + "/reports?page=" + data.page + "&size=" + data.size);
  107. _httpRequest.ContentType = "application/json";
  108. _httpRequest.Method = "GET";
  109. _httpRequest.Headers.Add("Authorization", auth);
  110. var _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
  111. using (var _streamReader = new StreamReader(_httpResponse.GetResponseStream()))
  112. {
  113. str = _streamReader.ReadToEnd();
  114. _reportedFeedResponse = new JavaScriptSerializer().Deserialize<List<ReportedFeedResponse>>(str);
  115. dbResult.SetError("0", "Success", "");
  116. }
  117. _httpResponse.Close();
  118. Utility.LogResponse(logId, str, dbResult.ErrorCode, dbResult.Msg);
  119. }
  120. catch (Exception ex)
  121. {
  122. dbResult.SetError("1", ex.Message, "");
  123. str = serializer.Serialize(dbResult);
  124. }
  125. err = dbResult;
  126. return _reportedFeedResponse;
  127. }
  128. public string BlockUnblockFeed(BlockUnblockFeedParameters data,out DbResult err)
  129. {
  130. var result = "";
  131. try
  132. {
  133. var auth = Utility.GetSocialWallApiAuthKey();
  134. JavaScriptSerializer serializer = new JavaScriptSerializer();
  135. var _request = serializer.Serialize(data);
  136. _dbResult = Utility.LogRequest(data.userId, providerName, "GetFeedReportDetails", data.feedId, _request);
  137. logId = _dbResult.Id;
  138. using (var client = new WebClient())
  139. {
  140. client.Headers.Add("Content-Type:application/json");
  141. client.Headers.Add("Authorization", auth);
  142. result = client.UploadString(_baseURL + "/feeds/" + data.feedId + "/blocks", "PATCH", serializer.Serialize(data));
  143. dbResult.SetError("0", "Feed block/unblock Successfully!!!", "");
  144. }
  145. Utility.LogResponse(logId, result, dbResult.ErrorCode, dbResult.Msg);
  146. }
  147. catch (WebException wex)
  148. {
  149. if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.InternalServerError)
  150. {
  151. dbResult.SetError("1", "Provided feed doesn't exist or it may have been deleted or you are not authorized to perform this action.", "");
  152. }
  153. else
  154. {
  155. dbResult.SetError("0", "Oops Something Went Wrong!!!", "");
  156. }
  157. }
  158. err = dbResult;
  159. return result;
  160. }
  161. }
  162. }