Exemplo n.º 1
0
        public VoipSettings GetSettings(string number)
        {
            try
            {
                var request = new ApiRequest(string.Format("crm/voip/numbers/{0}", number), Cookie)
                    {
                        Method = HttpMethod.Get,
                        ResponseType = ResponseType.Json
                    };

                return Newtonsoft.Json.JsonConvert.DeserializeObject<VoipPhone>(ApiClient.GetResponse(request).Response).Settings;
            }
            catch (Exception e)
            {
                Log.ErrorFormat("GetSettings: StackTrace:{0}, Message: {1}", e.StackTrace, e.Message);
                throw;
            }
        }
Exemplo n.º 2
0
        private void ChangeApiStatus(int status, string userId)
        {
            try
            {
                var userAccount = GetUserAccount();
                if (userAccount == null) return;

                var tenant = CoreContext.TenantManager.GetTenant(userAccount.Tenant);
                var cookie = (string)Context.Request.Environment["server.UserCookie"];

                var apiClient = new ApiClient(tenant.GetTenantDomain());
                var request = new ApiRequest(string.Format("crm/voip/opers/{0}", userId), cookie)
                    {
                        Method = HttpMethod.Put
                    };
                request.Parameters.Add(new RequestParameter {Name = "status", Value = status});
                apiClient.GetResponse(request);
            }
            catch(ApiErrorException e)
            {
                Log.ErrorFormat("ChangeApiStatus userId: {0}, ErrorStackTrace: {1}, ErrorMessage:{2}", userId, e.ErrorStackTrace, e.ErrorMessage);
                throw;
            }
            catch(Exception e)
            {
                Log.Error("ChangeApiStatus userId:" + userId, e);
                throw;
            }
        }
Exemplo n.º 3
0
        private void SaveCall(string callId, string answeredBy, int status)
        {
            var userAccount = GetUserAccount();
            if (userAccount == null) return;

            var tenant = CoreContext.TenantManager.GetTenant(userAccount.Tenant);
            var cookie = (string)Context.Request.Environment["server.UserCookie"];

            var apiClient = new ApiClient(tenant.GetTenantDomain());
            var request = new ApiRequest(string.Format("crm/voip/call/{0}", callId), cookie)
                {
                    Method = HttpMethod.Post,
                    ResponseType = ResponseType.Json
                };

            request.Parameters.Add(new RequestParameter {Name = "answeredBy", Value = answeredBy});
            request.Parameters.Add(new RequestParameter {Name = "status", Value = status});

            apiClient.GetResponse(request);
        }
Exemplo n.º 4
0
        private string GetCall(string callId)
        {
            var userAccount = GetUserAccount();
            if (userAccount == null) return null;

            var tenant = CoreContext.TenantManager.GetTenant(userAccount.Tenant);
            var cookie = (string)Context.Request.Environment["server.UserCookie"];

            var apiClient = new ApiClient(tenant.GetTenantDomain());
            var request = new ApiRequest(string.Format("crm/voip/call/{0}", callId), cookie)
                {
                    Method = HttpMethod.Get,
                    ResponseType = ResponseType.Json
                };

            return apiClient.GetResponse(request).Response;
        }
Exemplo n.º 5
0
        public JObject SaveOrUpdateCall(string callId, string from = null, string to = null, string answeredBy = null,
                                        string dialDuration = null, string recordUrl = null,
                                        string recordDuration = null, VoipCallStatus? status = null,
                                        string contactId = null, decimal? price = null)
        {
            try
            {
                var request = new ApiRequest(string.Format("crm/voip/call/{0}", callId), Cookie)
                    {
                        Method = HttpMethod.Post,
                        ResponseType = ResponseType.Json
                    };

                if (!string.IsNullOrEmpty(from))
                    request.Parameters.Add(new RequestParameter {Name = "from", Value = from});

                if (!string.IsNullOrEmpty(to))
                    request.Parameters.Add(new RequestParameter {Name = "to", Value = to});

                if (status != null)
                    request.Parameters.Add(new RequestParameter {Name = "status", Value = status.Value});

                if (!string.IsNullOrEmpty(dialDuration))
                    request.Parameters.Add(new RequestParameter {Name = "dialDuration", Value = dialDuration});

                if (!string.IsNullOrEmpty(recordUrl))
                    request.Parameters.Add(new RequestParameter {Name = "recordUrl", Value = recordUrl});

                if (!string.IsNullOrEmpty(recordDuration))
                    request.Parameters.Add(new RequestParameter {Name = "recordDuration", Value = recordDuration});

                if (!string.IsNullOrEmpty(answeredBy))
                    request.Parameters.Add(new RequestParameter {Name = "answeredBy", Value = answeredBy});

                if (!string.IsNullOrEmpty(contactId))
                    request.Parameters.Add(new RequestParameter {Name = "contactId", Value = contactId});

                if (price.HasValue)
                    request.Parameters.Add(new RequestParameter { Name = "price", Value = price.Value.ToString(CultureInfo.InvariantCulture) });

                return JObject.Parse(ApiClient.GetResponse(request).Response);
            }
            catch (ApiErrorException e)
            {
                Log.ErrorFormat("SaveOrUpdateCall: StackTrace:{0}, Message: {1}", e.ErrorStackTrace, e.ErrorMessage);
                throw;
            }
        }
Exemplo n.º 6
0
        public ApiResponse GetResponse(ApiRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            HttpWebRequest webRequest = CreateWebRequest(request);
            HttpWebResponse webResponse;
            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException exception)
            {
                if (exception.Response == null)
                {
                    throw;
                }

                webResponse = (HttpWebResponse)exception.Response;
                if (new ContentType(webResponse.ContentType).MediaType == "text/html") // generic http error
                {
                    throw new HttpErrorException((int)webResponse.StatusCode, webResponse.StatusDescription);
                }
            }

            using (webResponse)
            using (var responseStream = webResponse.GetResponseStream())
            {
                var mediaType = new ContentType(webResponse.ContentType).MediaType;
                switch (mediaType)
                {
                    case "application/json":
                        return ResponseParser.ParseJsonResponse(responseStream);
                    case "text/xml":
                        return ResponseParser.ParseXmlResponse(responseStream);
                    default:
                        throw new UnsupportedMediaTypeException(mediaType);
                }
            }
        }
Exemplo n.º 7
0
        public JObject SaveOrUpdateCallHistory(string callId, string parentCallId, string answeredBy = null, DateTime? queueDate = null,
            DateTime? answerDate = null, DateTime? endDialDate = null, string recordUrl = null, string recordDuration = null, decimal? price = null)
        {
            try
            {
                var request = new ApiRequest(string.Format("crm/voip/callhistory/{0}", callId), Cookie)
                {
                    Method = HttpMethod.Post,
                    ResponseType = ResponseType.Json
                };

                request.Parameters.Add(new RequestParameter { Name = "parentCallId", Value = parentCallId });

                if (!string.IsNullOrEmpty(answeredBy))
                    request.Parameters.Add(new RequestParameter { Name = "answeredBy", Value = answeredBy });

                if (queueDate.HasValue)
                    request.Parameters.Add(new RequestParameter { Name = "queueDate", Value = queueDate.Value });

                if (answerDate.HasValue)
                    request.Parameters.Add(new RequestParameter { Name = "answerDate", Value = answerDate.Value });

                if (endDialDate.HasValue)
                    request.Parameters.Add(new RequestParameter { Name = "endDialDate", Value = endDialDate.Value });

                if (!string.IsNullOrEmpty(recordUrl))
                    request.Parameters.Add(new RequestParameter { Name = "recordUrl", Value = recordUrl });

                if (!string.IsNullOrEmpty(recordDuration))
                    request.Parameters.Add(new RequestParameter { Name = "recordDuration", Value = recordDuration });

                if (price.HasValue)
                    request.Parameters.Add(new RequestParameter { Name = "price", Value = price.Value.ToString(CultureInfo.InvariantCulture) });

                return JObject.Parse(ApiClient.GetResponse(request).Response);
            }
            catch (ApiErrorException e)
            {
                Log.ErrorFormat("SaveOrUpdateCallHistory: StackTrace:{0}, Message: {1}", e.ErrorStackTrace, e.ErrorMessage);
            }
            catch (Exception e)
            {
                Log.ErrorFormat("SaveOrUpdateCallHistory: StackTrace:{0}, Message: {1}", e.StackTrace, e.Message);
            }

            return new JObject();
        }
Exemplo n.º 8
0
        private void WriteUrlencoded(HttpWebRequest httpRequest, ApiRequest request)
        {
            httpRequest.ContentType = "application/x-www-form-urlencoded";

            using (var requestStream = httpRequest.GetRequestStream())
            using (var streamWriter = new StreamWriter(requestStream))
            {
                streamWriter.Write(CreateQuery(request));
            }
        }
Exemplo n.º 9
0
 private static IEnumerable<RequestParameter> EnumerateParameters(ApiRequest request)
 {
     foreach (var parameter in request.Parameters.Where(parameter => !string.IsNullOrEmpty(parameter.Name) && parameter.Value != null))
     {
         var enumerable = parameter.Value as IEnumerable;
         if (enumerable != null && !(parameter.Value is string))
         {
             foreach (var value in enumerable)
             {
                 yield return new RequestParameter {Name = parameter.Name + "[]", Value = value};
             }
         }
         else if (parameter.Value is DateTime)
         {
             yield return new RequestParameter {Name = parameter.Name, Value = ((DateTime)parameter.Value).ToString("o")};
         }
         else
         {
             yield return parameter;
         }
     }
 }
Exemplo n.º 10
0
        private void WriteMultipart(HttpWebRequest httpRequest, ApiRequest request)
        {
            string boundary = DateTime.UtcNow.Ticks.ToString("x");

            httpRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            using (var requestStream = httpRequest.GetRequestStream())
            {
                foreach (var parameter in EnumerateParameters(request))
                {
                    requestStream.WriteString("\r\n--{0}\r\nContent-Disposition: form-data; name=\"{1}\";\r\n\r\n{2}", boundary, parameter.Name, parameter.Value);
                }

                foreach (var file in request.Files)
                {
                    requestStream.WriteString("\r\n--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\nContent-Transfer-Encoding: binary\r\n\r\n",
                                              boundary,
                                              Path.GetFileNameWithoutExtension(file.Name),
                                              Path.GetFileName(file.Name),
                                              !string.IsNullOrEmpty(file.ContentType) ? file.ContentType : "application/octet-stream");

                    if (file.Data.CanSeek)
                    {
                        file.Data.Seek(0, SeekOrigin.Begin);
                    }
                    file.Data.CopyTo(requestStream);
                    if (file.CloseStream)
                    {
                        file.Data.Close();
                    }
                }

                requestStream.WriteString("\r\n--{0}--\r\n", boundary);
            }
        }
Exemplo n.º 11
0
        private string CreateQuery(ApiRequest request)
        {
            var sb = new StringBuilder();
            foreach (var parameter in EnumerateParameters(request))
            {
                sb.AppendFormat("{0}={1}&", parameter.Name, parameter.Value);
            }

            if (sb.Length > 0) 
                sb.Remove(sb.Length - 1, 1);
            
            return sb.ToString();
        }
Exemplo n.º 12
0
 private string CreatePath(ApiRequest request)
 {
     return ApiRoot + "/" + request.Url + "." + request.ResponseType.ToString().ToLower();
 }
Exemplo n.º 13
0
        private HttpWebRequest CreateWebRequest(ApiRequest request)
        {
            var uri = new UriBuilder(UriScheme.ToString().ToLower(), Host) {Path = CreatePath(request)};

            if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Delete)
            {
                uri.Query = CreateQuery(request);
            }
            
            var httpRequest = (HttpWebRequest)WebRequest.Create(uri.ToString());
            httpRequest.Method = request.Method.ToString().ToUpper();
            httpRequest.AllowAutoRedirect = true;

            if (!string.IsNullOrWhiteSpace(request.AuthToken))
            {
                httpRequest.Headers["Authorization"] = request.AuthToken;
            }
            httpRequest.Headers.Add(request.Headers);

            if (request.Method != HttpMethod.Get && request.Method != HttpMethod.Delete)
            {
                if (request.Files.Any() || request.RequestType == RequestType.Multipart)
                {
                    WriteMultipart(httpRequest, request);
                }
                else
                {
                    WriteUrlencoded(httpRequest, request);
                }
            }

            return httpRequest;
        }
Exemplo n.º 14
0
        public ApiResponse GetResponse(ApiRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            HttpWebRequest webRequest = CreateWebRequest(request);
            HttpWebResponse webResponse;
            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException exception)
            {
                if (exception.Response == null)
                    throw;

                webResponse = (HttpWebResponse)exception.Response;
                if (new ContentType(webResponse.ContentType).MediaType == "text/html") // generic http error
                    throw new HttpErrorException((int)webResponse.StatusCode, webResponse.StatusDescription);
            }

            using (webResponse)
            using (var responseStream = webResponse.GetResponseStream())
            {
                if (new ContentType(webResponse.ContentType).MediaType == "application/json")
                {
                    return ResponseParser.ParseJsonResponse(responseStream);
                }

                return ResponseParser.ParseXmlResponse(responseStream);
            }
        }