예제 #1
0
        /// <summary>
        /// Requests for available tickets
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="destination"></param>
        /// <param name="date"></param>
        /// <param name="returning_date"></param>
        /// <param name="adult_count"></param>
        /// <param name="child_count"></param>
        /// <returns>Available tickets list</returns>
        public async Task <QPXTrip[]> RequestTicketsAsync(string origin, string destination, DateTime date, DateTime returning_date, uint adult_count, uint child_count)
        {
            JObject req_obj = new JObject
                              (
                new JProperty("request",
                              new JObject(
                                  new JProperty("passengers",
                                                new JObject(
                                                    new JProperty("kind", "qpxexpress#passengerCounts"),
                                                    new JProperty("adultCount", adult_count),
                                                    new JProperty("childCount", child_count)
                                                    )
                                                ),
                                  new JProperty("slice",
                                                new JArray(
                                                    new JObject(
                                                        new JProperty("kind", "qpxexpress#sliceInput"),
                                                        new JProperty("origin", origin),
                                                        new JProperty("destination", destination),
                                                        new JProperty("date", date.ToString("yyyy-MM-dd"))
                                                        ),
                                                    new JObject(
                                                        new JProperty("kind", "qpxexpress#sliceInput"),
                                                        new JProperty("origin", destination),
                                                        new JProperty("destination", origin),
                                                        new JProperty("date", returning_date.ToString("yyyy-MM-dd"))
                                                        )
                                                    )
                                                )
                                  )
                              )
                              );

            byte[] req_data;

            using (var stream = new MemoryStream())
            {
                using (var jwriter = new JsonTextWriter(new StreamWriter(stream)))
                {
                    req_obj.WriteTo(jwriter);
                }
                req_data = stream.ToArray();
            }

            NameValueCollection parameters = new NameValueCollection();

            parameters.Add("key", "AIzaSyAQ1FVevSqFvWJUGSeF26WDVqTSU-qDpIQ");

            NameValueCollection headers = new NameValueCollection();

            headers.Add("Content-Type", "application/json");

            byte[] resp_data = await HttpManager.PostAsync(endpoint, api, version, "trips/search",
                                                           parameters, null, headers, req_data);

            JObject resp_obj;

            using (var stream = new MemoryStream(resp_data))
            {
                using (var reader = new JsonTextReader(new StreamReader(stream)))
                {
                    resp_obj = (JObject)JToken.ReadFrom(reader);
                }
            }

            var trips = from trip in resp_obj["trips"]["tripOption"]
                        select new QPXTrip
            {
                Cost     = parsePrice(trip["saleTotal"].ToString()),
                Currency = parseCurrency(trip["saleTotal"].ToString()),
                Code     = trip["id"].ToString(),
                There    = createTicket(trip["slice"].First, trip["pricing"].First["saleTotal"].ToString()),
                Back     = createTicket(trip["slice"].ElementAt(1), trip["pricing"].First["saleTotal"].ToString())
            };

            return(trips.ToArray());
        }