Example #1
0
        public void CheckThrowsErrorTest()
        {
            var request = new NumberPortabilityRequest
            {
                TnList = new[] { "1111" }
            };
            var response = new BandwidthIrisException(
                "170",
                "error thrown",
                System.Net.HttpStatusCode.ExpectationFailed
                );

            using (var server = new HttpServer(new RequestHandler
            {
                EstimatedMethod = "POST",
                EstimatedPathAndQuery = string.Format("/v1.0/accounts/{0}/lnpchecker?fullCheck=true", Helper.AccountId),
                EstimatedContent = Helper.ToXmlString(request),
                ContentToSend = new StringContent("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><NumberPortabilityResponse><PortableNumbers><Tn>9192202164</Tn><Tn>9197891146</Tn></PortableNumbers><PortabilityErrors><Error><Code>7331</Code><Description>Rate Center Not Present in Bandwidth Dashboard</Description><TelephoneNumbers><Tn>5555555555</Tn></TelephoneNumbers></Error></PortabilityErrors><SupportedRateCenters><RateCenterGroup><RateCenter>DURHAM</RateCenter><City>DURHAM</City><State>NC</State><LATA>426</LATA><Tiers><Tier>0</Tier></Tiers><TnList><Tn>9192202164</Tn></TnList></RateCenterGroup><RateCenterGroup><RateCenter>RALEIGH</RateCenter><City>RALEIGH</City><State>NC</State><LATA>426</LATA><Tiers><Tier>0</Tier></Tiers><TnList><Tn>9197891146</Tn></TnList></RateCenterGroup></SupportedRateCenters><UnsupportedRateCenters/></NumberPortabilityResponse>", Encoding.UTF8, "application/xml")
            }))
            {
                var client = Helper.CreateClient();
                try
                {
                    var result = LnpChecker.Check(client, new[] { "1111" }, true).Result;
                } catch (BandwidthIrisException e)
                {
                    return;
                }
                catch (AggregateException e)
                {
                    Exception innerEx = e;
                    while (innerEx != null)
                    {
                        string mesg = innerEx.Message;
                        innerEx = innerEx.InnerException;
                        Console.WriteLine(mesg);
                    }
                    return;
                }
                catch (Exception e)
                {
                    return;
                }
                Assert.Fail("The exception was not thrown");
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting the Phone Number Ordering App");

            useHttp(true); //not https
            port("8080");
            startServerInstance();

            post("/subscriptions/orders", (EagleRequest request, HttpListenerResponse response) =>
            {
                XmlSerializer serializer  = new XmlSerializer(typeof(Notification));
                Notification notification = (Notification)serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(request.Body)));
                Console.WriteLine(notification.Status);
                Console.WriteLine(notification.Message);
            });

            post("/subscriptions/disconnects", (EagleRequest request, HttpListenerResponse response) =>
            {
                XmlSerializer serializer  = new XmlSerializer(typeof(Notification));
                Notification notification = (Notification)serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(request.Body)));
                Console.WriteLine(notification.Status);
                Console.WriteLine(notification.Message);

                var phoneNumber = notification.CompletedTelephoneNumbers[0];

                storage.Remove(phoneNumber);
            });

            get("/availablePhoneNumbers", (EagleRequest request, HttpListenerResponse response) =>
            {
                var queryParams = request.RawRequest.QueryString;
                var query       = new Dictionary <string, object>()
                {
                    { "quantity", 10 },
                };

                if (queryParams.Get("zipCode") != null)
                {
                    query.Add("zip", queryParams.Get("zipCode"));
                }

                if (queryParams.Get("areaCode") != null)
                {
                    query.Add("areaCode", queryParams.Get("areaCode"));
                }


                try
                {
                    var res = AvailableNumbers.List(client, query).Result;
                    return(res.TelephoneNumberList);
                } catch (AggregateException ex)
                {
                    if (ex.InnerException is BandwidthIrisException)
                    {
                        response.StatusCode           = 400;
                        BandwidthIrisException irisEx = (BandwidthIrisException)ex.InnerException;
                        return(new Error
                        {
                            BandwidthErrorCode = irisEx.Code,
                            BandwidthErrorDescription = irisEx.Body.ToString(),
                            Description = "Bandwidth Invalid User Input",
                            Type = "validation"
                        });
                    }
                    throw ex;
                }
            });

            post("/phoneNumbers", (EagleRequest request, HttpListenerResponse response) =>
            {
                string phoneNumber = request.Body.phoneNumber;

                if (storage.ContainsKey(phoneNumber))
                {
                    return(new Error
                    {
                        Type = "owned number",
                        Description = "You have already ordered this number."
                    });
                }

                OrderResult orderResult;
                try
                {
                    orderResult = Order.Create(client, new Order
                    {
                        CustomerOrderId = "customerOrderId",
                        SiteId          = SITE_ID, //The site to order the number for
                        ExistingTelephoneNumberOrderType = new ExistingTelephoneNumberOrderType
                        {
                            TelephoneNumberList = new string[] { phoneNumber }
                        }
                    }).Result;
                } catch (AggregateException ex)
                {
                    if (ex.InnerException is BandwidthIrisException)
                    {
                        response.StatusCode           = 400;
                        BandwidthIrisException irisEx = (BandwidthIrisException)ex.InnerException;
                        return(new Error
                        {
                            BandwidthErrorCode = irisEx.Code,
                            BandwidthErrorDescription = irisEx.Body.ToString(),
                            Description = "Bandwidth Invalid User Input",
                            Type = "validation"
                        });
                    }
                    throw ex;
                }

                var orderIdentifier = new OrderIdentifier
                {
                    OrderId     = orderResult.Order.OrderId,
                    PhoneNumber = phoneNumber
                };
                storage.Add(phoneNumber, orderIdentifier);
                response.StatusCode = 201;
                return(orderIdentifier);
            });

            get("/phoneNumbers", (EagleRequest request, HttpListenerResponse response) =>
            {
                return(storage.Values);
            });

            delete("/phoneNumbers/{phoneNumber}", (EagleRequest request, HttpListenerResponse response) =>
            {
                string phoneNumber = request.PathInfo.PathParameters.phoneNumber;

                if (!storage.ContainsKey(phoneNumber))
                {
                    response.StatusCode = 404;
                    return(new Error
                    {
                        Description = "This number has not ordered yet, cannot remove.",
                        Type = "not found"
                    });
                }

                try
                {
                    Disconnect.Create(client, "orderName", phoneNumber);
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerException is BandwidthIrisException)
                    {
                        response.StatusCode           = 400;
                        BandwidthIrisException irisEx = (BandwidthIrisException)ex.InnerException;
                        return(new Error
                        {
                            BandwidthErrorCode = irisEx.Code,
                            BandwidthErrorDescription = irisEx.Body.ToString(),
                            Description = "Bandwidth Invalid User Input",
                            Type = "validation"
                        });
                    }
                    throw ex;
                }
                response.StatusCode = 201;
                return(new Dictionary <string, bool>()
                {
                    { "Recieved", true }
                });
            });

            post("/stop", (EagleRequest request, HttpListenerResponse response) => {
                stop();
                return("Server Shutting Down");
            });

            Console.WriteLine("Server is Ready!!!!!!!!!");
            WaitOnServerToStop();
        }