using Common.Models.RequestResponse; using Common.Models.SendSMS; using Common.Utility; using log4net; using System; using System.Net.Http; using System.Net.Http.Headers; namespace Business.BusinessLogic.SMSApiService { public class SMSService : ISMSService { private readonly ILog _log = LogManager.GetLogger(typeof(SMSService)); private readonly string _baseUrl; private readonly string _baseUrlStatus; private readonly string _userName; private readonly string _password; private readonly string _senderId; private readonly string _langType; public SMSService() { _baseUrl = GetStatic.ReadWebConfig("onewaysmsURL", ""); _baseUrlStatus = GetStatic.ReadWebConfig("onewaysmsURLStatus", ""); _userName = GetStatic.ReadWebConfig("onewaysmsUserName", ""); _password = GetStatic.ReadWebConfig("onewaysmsPassword", ""); _senderId = GetStatic.ReadWebConfig("onewaysmsSenderId", ""); _langType = GetStatic.ReadWebConfig("onewaysmsLangType", ""); } public TPResponse SMSApiMethod(SMSRequestModel model) { TPResponse _tPResponse = new TPResponse() { ResponseCode = "1" }; string _qs = ""; if (model.method.ToLower() == "send") { if (model.MobileNumber.Replace("+", "").Equals("817044652959")) { _qs = "?apiusername=" + _userName + "&apipassword=" + _password + "&mobileno=" + model.MobileNumber; _qs += "&senderid=" + _senderId + "&languagetype=" + _langType + "&message=" + model.SMSBody + " dt: " + DateTime.Now.ToString("yyyyMMddhmmss"); ; } else { _qs = "?apiusername=" + _userName + "&apipassword=" + _password + "&mobileno=" + model.MobileNumber; _qs += "&senderid=" + _senderId + "&languagetype=" + _langType + "&message=" + model.SMSBody; } _tPResponse = CallTPApi(_qs, "get", model.method, _baseUrl); } else if (model.method.ToLower() == "status") { _qs = "?mtid=" + model.MTID; _tPResponse = CallTPApi(_qs, "get", model.method, _baseUrlStatus); } return _tPResponse; } public TPResponse CallTPApi(string _apiUrl, string _methodType, string _requestType, string _baseURLAPI) { _log.Debug("Calling API for " + _requestType + " with request:" + _apiUrl); TPResponse _tPResponse = new TPResponse() { ResponseCode = "1" }; using (HttpClient httpClient = new HttpClient()) { try { //var _response = ""; //HttpWebRequest _httpRequest = (HttpWebRequest)WebRequest.Create(_baseURLAPI + _apiUrl); //_httpRequest.ContentType = "application/json"; //_httpRequest.Method = "GET"; //var _httpResponse = (HttpWebResponse)_httpRequest.GetResponse(); //using (var _streamReader = new StreamReader(_httpResponse.GetResponseStream())) //{ // _response = _streamReader.ReadToEnd().ToString(); //} //_log.Debug(_response); //_tPResponse = ParseResponseMsg(_response, _requestType); //return _tPResponse; httpClient.BaseAddress = new Uri(_baseURLAPI); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-formurlencoded")); short timeOut = Convert.ToInt16(GetStatic.ReadWebConfig("transfast_apiTimeOutInSecond", "100")); httpClient.Timeout = new TimeSpan(0, 0, timeOut); HttpResponseMessage resp = new HttpResponseMessage(); if (_methodType.ToLower().Equals("get")) { _log.Debug("Initiating the call"); resp = httpClient.GetAsync(_apiUrl).Result; _log.Debug("Call finished"); } string result = resp.Content.ReadAsStringAsync().Result; _log.Debug(resp); if (resp.IsSuccessStatusCode) { _tPResponse = ParseResponseMsg(result, _requestType); return _tPResponse; } else { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "Error sending request!"; return _tPResponse; } } catch (HttpRequestException ex) { _tPResponse.ResponseCode = "999"; _tPResponse.Msg = ex.Message; _tPResponse.Data = ex.Data; } }; return _tPResponse; } private TPResponse ParseResponseMsg(string result, string requestType) { TPResponse _tPResponse = new TPResponse() { ResponseCode = "1" }; long respCode = GetStatic.ParseLong(result); if (requestType.ToLower() == "send") { if (respCode > 0) { _tPResponse.ResponseCode = "0"; _tPResponse.Msg = "SMS Sent Successfully!"; _tPResponse.Extra = result; } else if (respCode == -100) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "API Password or Username invalid!"; _tPResponse.Extra = result; } else if (respCode == -200) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "Sendr id is invalid!"; _tPResponse.Extra = result; } else if (respCode == -300) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "Mobile number is invalid!"; _tPResponse.Extra = result; } else if (respCode == -400) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "Language type is invalid!"; _tPResponse.Extra = result; } } else if (requestType.ToLower() == "status") { if (respCode == 0) { _tPResponse.ResponseCode = "0"; _tPResponse.Msg = "Success receive on mobile handset!"; _tPResponse.Extra = result; } else if (respCode == 100) { _tPResponse.ResponseCode = "0"; _tPResponse.Msg = "Message delivered to Telco!"; _tPResponse.Extra = result; } else if (respCode == -100) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "mtid invalid/not found! Response Code: " + respCode.ToString(); _tPResponse.Extra = result; } else if (respCode == -200) { _tPResponse.ResponseCode = "1"; _tPResponse.Msg = "Message sending fail! Response Code: " + respCode.ToString(); _tPResponse.Extra = result; } } return _tPResponse; } } }