예제 #1
0
        /// <summary>
        /// Voxity page asking end-user to allow your app to get there personnal informations. Requirements : HTTP listen server. Refer to : <see href="https://api.voxity.fr/doc/#api-Token-GetAuthorization"/>
        /// </summary>
        /// <returns><c>HTTP/1.1 200 OK</c></returns>
        public Uri AskAuthorization()
        {
            Dictionary <string, string> values = new Dictionary <string, string>();

            values.Add("redirect_uri", tokenSession.UriRedirect);
            values.Add("response_type", "code");
            values.Add("client_id", tokenSession.ClientId);

            HttpResponseMessage response = tokenSession.Request(ApiSession.HttpMethod.Get, "dialog/authorize", urlParams: values, isTokenRequired: false);

            if (response.IsSuccessStatusCode)
            {
                return(response.RequestMessage.RequestUri);
            }
            else
            {
                throw new HttpRequestException(response.StatusCode.ToString());
            }
        }
예제 #2
0
        /// <summary>
        /// [BETA] Authentificate user. Log user designated by username and password provided in params. Refer to : <see href="https://api.voxity.fr/doc/#api-Authentication-PostLogin"/>
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns>
        /// <c>HTTP 302 Redirect /</c> if success
        /// <c>HTTP 302 Redirect /login</c> if error
        /// </returns>
        public void Authenticate(string user, System.Security.SecureString password)
        {
            string decrypt = null;

            if (password == null)
            {
                throw new ArgumentNullException("securePassword");
            }

            IntPtr unmanagedString = IntPtr.Zero;

            try
            {
                unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(password);
                decrypt         = Marshal.PtrToStringUni(unmanagedString);
            }
            finally
            {
                Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
            }

            Dictionary <string, string> values = new Dictionary <string, string>();

            values.Add("username", user);
            values.Add("password", decrypt);

            HttpResponseMessage response = authenticationSession.Request(ApiSession.HttpMethod.Post, "login", urlParams: values, isTokenRequired: false);

            switch (response.StatusCode)
            {
            case HttpStatusCode.Redirect:
                //TODO : Redirect to / if credential valid, /login if bad
                break;

            default:
                throw new HttpRequestException(response.StatusCode.ToString());
            }
        }
예제 #3
0
        /// <summary>
        /// List all devices and gave, for each one status of it. Status can be ringing, available, unavailable, in-use. Important note : Only admin user can be access to all devices of a pool. Refer to : <see href="https://api.voxity.fr/doc/#api-Device-ListDevice"/>
        /// </summary>
        /// <returns>List of <see cref="Device"/> object.</returns>
        public List <Device> DeviceList()
        {
            HttpResponseMessage response = deviceSession.Request(ApiSession.HttpMethod.Get, "devices");

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <ApiResponse <List <Device> > >(response.Content.ReadAsStringAsync().Result).Results());
            }
            else
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    throw new ApiAuthenticateException();

                case HttpStatusCode.InternalServerError:
                    throw new ApiInternalErrorException();

                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Send SMS. Refer to : <see href="https://api.voxity.fr/doc/#api-SMS-SendSms"/>
        /// </summary>
        /// <param name="content">Message content</param>
        /// <param name="phoneNumber">[FORMAT: /^[0-9]{ 10}$/] Phone number for the message receiver</param>
        /// <param name="emitter">(Optional) [FORMAT: /^([a-z]|[A-Z]){4,11}$/] The emitter's name. Be aware that the receiver cannot respond to the message if this field is specified.</param>
        /// <returns><see cref="NewSms"/> object.</returns>
        public NewSms SendMessage(string content, string phoneNumber, string emitter = null)
        {
            if (Utils.Filter.ValidPhone(phoneNumber) != true && Utils.Filter.ValidRac(phoneNumber) != true)
            {
                throw new ApiSessionException("Invalid phone number.");
            }

            CreateSms cs = new CreateSms();

            if (!string.IsNullOrWhiteSpace(phoneNumber))
            {
                cs.phone_number = phoneNumber;
            }

            if (!string.IsNullOrWhiteSpace(content))
            {
                cs.content = content;
            }

            if (!string.IsNullOrWhiteSpace(emitter))
            {
                cs.emitter = emitter;
            }

            HttpResponseMessage response = smsSession.Request(ApiSession.HttpMethod.Post, "sms", contentType: "application/json", contentValue: JsonConvert.SerializeObject(cs, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <ApiResponse <NewSms> >(response.Content.ReadAsStringAsync().Result).Results());
            }
            else
            {
                switch (response.StatusCode)
                {
                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }
예제 #5
0
        /// <summary>
        /// This endpoint return information concerning current user. Refer to : <see href="https://api.voxity.fr/doc/#api-Users-UsersSelf"/>
        /// </summary>
        /// <returns>User object</returns>
        public User WhoAmI()
        {
            HttpResponseMessage response = userSession.Request(ApiSession.HttpMethod.Get, "users/self");

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <ApiResponse <User> >(response.Content.ReadAsStringAsync().Result).Results());
            }
            else
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    throw new HttpRequestException(response.StatusCode.ToString());

                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }
예제 #6
0
        /// <summary>
        /// [BETA] Upload a vocal message in wave/wav/wma format, 5 megabytes max.
        /// Refer to : <see href="https://api.voxity.fr/doc/#api-VMS-UploadFile"/>
        /// </summary>
        /// <param name="file">The sound file to upload. Be aware that the file must not exceed 5 megabytes.
        ///     Example : <Example><c>@/home/path/to/my/file.wav;type=audio/wav</c></Example>
        /// </param>
        /// <param name="description">A file description. Example : <Example>My file</Example></param>
        /// <returns><see cref="UploadVms"/> object.</returns>
        public UploadVms UploadSoundFile(string file, string description)
        {
            Dictionary <string, string> values = new Dictionary <string, string>();

            values.Add("uploadedfile", file);
            values.Add("description", description);

            HttpResponseMessage response = vmsSession.Request(ApiSession.HttpMethod.Post, "vms/files", urlParams: values);

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <UploadVms>(response.Content.ReadAsStringAsync().Result));
            }
            else
            {
                switch (response.StatusCode)
                {
                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Call logs. Refer to : <see href="https://api.voxity.fr/doc/#api-Calls-GetCallLogs"/>
        /// </summary>
        /// <param name="fromDate">[FORMAT : YYYY-MM-DD HH:MM:SS] Date from which the research begins</param>
        /// <param name="toDate">[FORMAT : YYYY-MM-DD HH:MM:SS] Date to which the research ends</param>
        /// <param name="filterSrc">Caller number. Example : <c>String[] src = new String[] { "0627071995" };
        /// filter_src.AddRange(src);</c></param>
        /// <param name="filterDest">Called number. Example : <c>String[] dest = new String[] { "0627071995" };
        /// filter_dest.AddRange(dest);</c></param>
        /// <param name="sens">Applies a filter on the call sense : "incoming"|"outgoing"</param>
        /// <param name="lastDays">Retrieves all calls events of the X last_days. The use of this parameter unables the two other parameters : from_date, to_date.</param>
        /// <returns>List of <see cref="Log"/> object</returns>
        public List <Log> Logs(string fromDate = null, string toDate = null, StringCollection filterSrc = null, StringCollection filterDest = null, string sens = null, string lastDays = null)
        {
            string fSrc  = null;
            string fDest = null;

            Dictionary <string, string> values = new Dictionary <string, string>();

            if (fromDate != null)
            {
                values.Add("from_date", fromDate);
            }
            if (toDate != null)
            {
                values.Add("to_date", toDate);
            }

            if (filterSrc != null)
            {
                foreach (string src in filterSrc)
                {
                    if (filterSrc.Count > 1)
                    {
                        fSrc += ",";
                    }
                    fSrc += "source==" + src;
                }
            }

            if (filterDest != null)
            {
                foreach (string dest in filterDest)
                {
                    if (filterDest.Count > 1)
                    {
                        fDest += ",";
                    }
                    fDest += "destination==" + dest;
                }
            }

            if (filterDest == null && filterSrc != null)
            {
                values.Add("filter", fSrc);
            }
            if (filterDest != null && filterSrc == null)
            {
                values.Add("filter", fDest);
            }
            if (filterDest != null && filterSrc != null)
            {
                values.Add("filter", fSrc + "," + fDest);
            }

            if (sens != null)
            {
                values.Add("sens", sens);
            }
            if (lastDays != null)
            {
                values.Add("last_days", lastDays);
            }

            HttpResponseMessage response = callSession.Request(ApiSession.HttpMethod.Get, "calls/logs", urlParams: values);

            if (response.IsSuccessStatusCode)
            {
                // Affect access_token, refresh_token values
                return(JsonConvert.DeserializeObject <ApiResponse <List <Log> > >(response.Content.ReadAsStringAsync().Result).Results());
            }
            else
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                    string msgError = null;

                    List <LogError> errors = JsonConvert.DeserializeObject <ApiResponse <List <LogError> > >(response.Content.ReadAsStringAsync().Result).Errors();

                    foreach (LogError error in errors)
                    {
                        switch (error.param)
                        {
                        case "from_date":
                            msgError += "Argument error : fromDate";
                            break;

                        case "to_date":
                            msgError += "Argument error : toDate";
                            break;

                        case "filter":
                            msgError += "Argument error : filterSrc/filterDest";
                            break;

                        case "sens":
                            msgError += "Argument error : sens";
                            break;

                        case "last_days":
                            msgError += "Argument error : last_days";
                            break;
                        }

                        msgError += ", Description: " + error.msg + ", Value: " + error.value + Environment.NewLine;
                    }

                    throw new ApiArgumentException(msgError);

                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Create a contact. Refer to : <see href="https://api.voxity.fr/doc/#api-Contact-CreateContact" />
        /// </summary>
        /// <param name="cn">Contact's name</param>
        /// <param name="telNum">Contact's phone number</param>
        /// <param name="telNumRac">[FORMAT: /^*[0-9]{1,4}$/] Phone number shortcut. Requirement <c>telNum</c></param>
        /// <param name="mobile">Contact's mobile number</param>
        /// <param name="racMobile">[FORMAT: /^*[0-9]{1,4}$/] Mobile phone number shortcut. Requirement <c>mobile</c></param>
        /// <param name="mail">Contact's mail address</param>
        /// <returns><see cref="ManageContact"/> object</returns>
        public ManageContact CreateContact(string cn, string telNum = null, string telNumRac = null, string mobile = null, string racMobile = null, string mail = null)
        {
            Contact c = new Contact();

            if (!string.IsNullOrWhiteSpace(cn))
            {
                c.cn = cn;
            }
            else
            {
                throw new ApiSessionException("[Voxity API] - Create Contact\nContact name is null or empty, please give a valid contact name.");
            }

            if (!string.IsNullOrWhiteSpace(telNum))
            {
                if (Utils.Filter.ValidPhone(telNum) != true)
                {
                    throw new ApiSessionException("Invalid phone number.");
                }
                else
                {
                    c.telephoneNumber = telNum;
                }
            }

            if (!string.IsNullOrWhiteSpace(telNumRac))
            {
                if (Utils.Filter.ValidRac(telNumRac) != true)
                {
                    throw new ApiSessionException("Invalid phone alias.");
                }
                else
                {
                    c.phoneNumberRaccourci = telNumRac;
                }
            }

            if (!string.IsNullOrWhiteSpace(mobile))
            {
                if (Utils.Filter.ValidPhone(mobile) != true)
                {
                    throw new ApiSessionException("Invalid mobile number.");
                }
                else
                {
                    c.mobile = mobile;
                }
            }

            if (!string.IsNullOrWhiteSpace(racMobile))
            {
                if (Utils.Filter.ValidRac(racMobile) != true)
                {
                    throw new ApiSessionException("Invalid mobile alias.");
                }
                else
                {
                    c.employeeNumber = racMobile;
                }
            }

            if (!string.IsNullOrWhiteSpace(mail))
            {
                c.mail = mail;
            }

            if (c.telephoneNumber == null && c.mobile == null)
            {
                throw new ApiSessionException("[Voxity API] - Create Contact\nNo phone number and mobile set. Please add one of them.");
            }


            HttpResponseMessage response = contactSession.Request(ApiSession.HttpMethod.Post, "contacts", contentType: "application/json", contentValue: JsonConvert.SerializeObject(c, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            if (response.IsSuccessStatusCode)
            {
                // Affect access_token, refresh_token values
                return(JsonConvert.DeserializeObject <ApiResponse <ManageContact> >(response.Content.ReadAsStringAsync().Result).Results());
            }
            else
            {
                switch (response.StatusCode)
                {
                default:
                    throw new HttpRequestException(response.StatusCode.ToString());
                }
            }
        }