Exemplo n.º 1
0
        internal async Task EnableMms(IrisAuthData authData, MmsOptions options, MessagingApplication application,
                                      CancellationToken?cancellationToken = null)
        {
            if (options == null || !options.Enabled)
            {
                return;
            }
            var xml = new XDocument(
                new XElement("MmsFeature",
                             new XElement("MmsSettings",
                                          new XElement("protocol", "HTTP")
                                          ),
                             new XElement("Protocols",
                                          new XElement("HTTP",
                                                       new XElement("HttpSettings",
                                                                    new XElement("ProxyPeerId", "539692")
                                                                    )
                                                       )
                                          )
                             )
                );

            await MakeRequestAsync(authData, HttpMethod.Post,
                                   $"/sites/{authData.SubaccountId}/sippeers/{application.LocationId}/products/messaging/features/mms", xml, true,
                                   cancellationToken);
        }
Exemplo n.º 2
0
        internal async Task EnableSms(IrisAuthData authData, SmsOptions options, MessagingApplication application,
                                      CancellationToken?cancellationToken = null)
        {
            if (options == null || !options.Enabled)
            {
                return;
            }
            var xml = new XDocument(
                new XElement("SipPeerSmsFeature",
                             new XElement("SipPeerSmsFeatureSettings",
                                          new XElement("TollFree", options.TollFreeEnabled),
                                          new XElement("ShortCode", options.ShortCodeEnabled),
                                          new XElement("Protocol", "HTTP"),
                                          new XElement("Zone1", true),
                                          new XElement("Zone2", false),
                                          new XElement("Zone3", false),
                                          new XElement("Zone4", false),
                                          new XElement("Zone5", false)
                                          ),
                             new XElement("HttpSettings",
                                          new XElement("ProxyPeerId", "539692")
                                          )
                             )
                );

            await MakeRequestAsync(authData, HttpMethod.Post,
                                   $"/sites/{authData.SubaccountId}/sippeers/{application.LocationId}/products/messaging/features/sms", xml, true,
                                   cancellationToken);
        }
Exemplo n.º 3
0
        internal async Task AssignApplicationToLocation(IrisAuthData authData, MessagingApplication application,
                                                        CancellationToken?cancellationToken = null)
        {
            var xml = new XDocument(
                new XElement("ApplicationsSettings",
                             new XElement("HttpMessagingV2AppId", application.ApplicationId)
                             )
                );

            await MakeRequestAsync(authData, HttpMethod.Put,
                                   $"/sites/{authData.SubaccountId}/sippeers/{application.LocationId}/products/messaging/applicationSettings", xml,
                                   true, cancellationToken);
        }
Exemplo n.º 4
0
        public async Task <MessagingApplication> CreateMessagingApplicationAsync(IrisAuthData authData,
                                                                                 CreateMessagingApplicationData data, CancellationToken?cancellationToken = null)
        {
            var app = new MessagingApplication
            {
                ApplicationId = await CreateApplication(authData, data, cancellationToken),
                LocationId    = await CreateLocation(authData, data, cancellationToken)
            };

            await EnableSms(authData, data.SmsOptions, app, cancellationToken);
            await EnableMms(authData, data.MmsOptions, app, cancellationToken);
            await AssignApplicationToLocation(authData, app, cancellationToken);

            return(app);
        }
Exemplo n.º 5
0
        private HttpRequestMessage CreateIrisRequest(IrisAuthData authData, HttpMethod method, string path,
                                                     object query = null)
        {
            var url = new UriBuilder("https://dashboard.bandwidth.com")
            {
                Path  = $"/v1.0/api/accounts/{authData.AccountId}{path}",
                Query = Client.BuildQueryString(query)
            };
            var message = new HttpRequestMessage(method, url.Uri);

            message.Headers.UserAgent.Add(Client.UserAgent);
            message.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                          Convert.ToBase64String(Encoding.UTF8.GetBytes($"{authData.UserName}:{authData.Password}")));
            return(message);
        }
Exemplo n.º 6
0
        public async Task <string[]> SearchAndOrderNumbersAsync(IrisAuthData authData, MessagingApplication application,
                                                                SearchAndOrderNumbersQuery query, CancellationToken?cancellationToken = null)
        {
            var xml = new XDocument(
                new XElement("Order", query.ToXElement(),
                             new XElement("SiteId", authData.SubaccountId),
                             new XElement("PeerId", application.LocationId))
                );

            var(doc, _) = await MakeRequestAsync(authData, HttpMethod.Post, "/orders", xml, true, cancellationToken);

            var orderId         = doc.Descendants("Order").First().Element("id").Value;
            var successStatuses = new[] { "COMPLETE", "PARTIAL" };
            var stopwatch       = Stopwatch.StartNew();

            try
            {
                while (true)
                {
                    await Task.Delay(500, cancellationToken ?? default(CancellationToken));

                    if (stopwatch.Elapsed >= query.Timeout)
                    {
                        throw new TimeoutException();
                    }
                    var(result, _) = await MakeRequestAsync(authData, HttpMethod.Get, $"/orders/{orderId}", null, true,
                                                            cancellationToken);

                    var status = (result.Descendants("OrderStatus").FirstOrDefault() ?? new XElement("OrderStatus")).Value;
                    if (successStatuses.Contains(status))
                    {
                        return((result.Descendants("CompletedNumbers").FirstOrDefault() ?? new XElement("CompletedNumbers"))
                               .Descendants("FullNumber").Select(n => n.Value).ToArray());
                    }
                    if (status == "FAILED")
                    {
                        throw new BandwidthException("Error on reserving phone numbers", HttpStatusCode.BadRequest);
                    }
                }
            }
            finally
            {
                stopwatch.Stop();
            }
        }
Exemplo n.º 7
0
        internal async Task <string> CreateLocation(IrisAuthData authData, CreateMessagingApplicationData data,
                                                    CancellationToken?cancellationToken = null)
        {
            var xml = new XDocument(
                new XElement("SipPeer",
                             new XElement("PeerName", data.LocationName),
                             new XElement("IsDefaultPeer", data.IsDefaultLocation)
                             )
                );

            var(_, response) = await MakeRequestAsync(authData, HttpMethod.Post, $"/sites/{authData.SubaccountId}/sippeers",
                                                      xml, false, cancellationToken);

            using (response)
            {
                return((response.Headers.Location ?? new Uri("http://localhost")).AbsolutePath.Split('/').LastOrDefault());
            }
        }
Exemplo n.º 8
0
        internal async Task <string> CreateApplication(IrisAuthData authData, CreateMessagingApplicationData data,
                                                       CancellationToken?cancellationToken = null)
        {
            var xml = new XDocument(
                new XElement("Application",
                             new XElement("AppName", data.Name),
                             new XElement("CallbackUrl", data.CallbackUrl),
                             new XElement("CallBackCreds",
                                          data.CallbackAuthData == null
              ? null
              : new[]
            {
                new XElement("UserId", data.CallbackAuthData.UserName),
                new XElement("Password", data.CallbackAuthData.Password)
            })
                             )
                );

            var(doc, _) = await MakeRequestAsync(authData, HttpMethod.Post, "/applications", xml, true, cancellationToken);

            return(doc.Descendants("ApplicationId").First().Value);
        }
Exemplo n.º 9
0
        internal async Task <(XDocument, HttpResponseMessage)> MakeRequestAsync(IrisAuthData authData, HttpMethod method,
                                                                                string path, XDocument doc, bool disposeResponse = false, CancellationToken?cancellationToken = null)
        {
            var request = CreateIrisRequest(authData, method, path);

            if (doc != null)
            {
                var xml = doc.ToString();
                request.Content = new StringContent(xml, Encoding.UTF8, "application/xml");
            }
            var response = await Client.Http.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken);

            try
            {
                return(await CheckResponse(response), response);
            }
            finally
            {
                if (disposeResponse)
                {
                    response.Dispose();
                }
            }
        }