Пример #1
0
        //This is a sample console application that shows you how to grab a User token from AAD for the current user of the app
        //The same caveat remains, that the current user of the app needs to be part of either the Owner, Reader or Contributor role for the requested AzureSubID.
        static void Main(string[] args)
        {
            //Get the AAD User token to get authorized to make the call to the Usage API
            string token = GetOAuthTokenFromAAD();

            /*Setup API call to RateCard API
             * Callouts:
             * See the App.config file for all AppSettings key/value pairs
             * You can get a list of offer numbers from this URL: http://azure.microsoft.com/en-us/support/legal/offer-details/
             * You can configure an OfferID for this API by updating 'MS-AZR-{Offer Number}'
             * The RateCard Service/API is currently in preview; please use "2015-06-01-preview" or "2016-08-31-preview" for api-version (see https://msdn.microsoft.com/en-us/library/azure/mt219005 for details)
             * Please see the readme if you are having problems configuring or authenticating: https://github.com/Azure-Samples/billing-dotnet-ratecard-api
             */

            // Build up the HttpWebRequest
            string requestURL = String.Format("{0}/{1}/{2}/{3}",
                                              ConfigurationManager.AppSettings["ARMBillingServiceURL"],
                                              "subscriptions",
                                              ConfigurationManager.AppSettings["SubscriptionID"],
                                              "providers/Microsoft.Commerce/RateCard?api-version=2016-08-31-preview&$filter=OfferDurableId eq 'MS-AZR-0121p' and Currency eq 'USD' and Locale eq 'en-US' and RegionInfo eq 'US'");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);

            // Add the OAuth Authorization header, and Content Type header
            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
            request.ContentType = "application/json";

            // Call the RateCard API, dump the output to the console window
            try
            {
                // Call the REST endpoint
                Console.WriteLine("Calling RateCard service...");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine(String.Format("RateCard service response status: {0}", response.StatusDescription));
                Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader readStream       = new StreamReader(receiveStream, Encoding.UTF8);
                var          rateCardResponse = readStream.ReadToEnd();
                Console.WriteLine("RateCard stream received.  Press ENTER to continue with raw output.");
                Console.ReadLine();
                Console.WriteLine(rateCardResponse);
                Console.WriteLine("Raw output complete.  Press ENTER to continue with JSON output.");
                Console.ReadLine();

                // Convert the Stream to a strongly typed RateCardPayload object.
                // You can also walk through this object to manipulate the individuals member objects.
                RateCardPayload payload = JsonConvert.DeserializeObject <RateCardPayload>(rateCardResponse);
                Console.WriteLine(rateCardResponse.ToString());
                response.Close();
                readStream.Close();
                Console.WriteLine("JSON output complete.  Press ENTER to close.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("{0} \n\n{1}", e.Message, e.InnerException != null ? e.InnerException.Message : ""));
                Console.ReadLine();
            }
        }
        private static void ProcessOffer(string token, string offerName)
        {
            var apiVersion = ConfigurationManager.AppSettings["ApiVersion"];
            var currency   = ConfigurationManager.AppSettings["Currency"];
            var regionInfo = ConfigurationManager.AppSettings["RegionInfo"];
            var url        = string.Format("providers/Microsoft.Commerce/RateCard?api-version={0}&$filter=OfferDurableId eq '{1}' and Currency eq '{2}' and Locale eq 'en-US' and RegionInfo eq '{3}'",
                                           apiVersion, offerName, currency, regionInfo);
            // Build up the HttpWebRequest
            string requestURL = String.Format("{0}/{1}/{2}/{3}",
                                              ConfigurationManager.AppSettings["ARMBillingServiceURL"],
                                              "subscriptions",
                                              ConfigurationManager.AppSettings["SubscriptionID"],
                                              url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);

            // Add the OAuth Authorization header, and Content Type header
            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
            // request.Date = DateTime.UtcNow;
            request.ContentType = "application/json";

            // Call the RateCard API, dump the output to the console window

            // Call the REST endpoint
            Console.WriteLine("Calling RateCard service...");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(String.Format("RateCard service response status: {0}", response.StatusDescription));
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format.
            using (var readStream = new StreamReader(receiveStream, Encoding.UTF8)) {
                var rateCardResponse = readStream.ReadToEnd();
#if SHOW_OUTPUT
                Console.WriteLine("RateCard stream received.  Press ENTER to continue with raw output.");
                Console.ReadLine();
                Console.WriteLine(rateCardResponse);
                Console.WriteLine("Raw output complete.  Press ENTER to continue with JSON output.");
                Console.ReadLine();
#endif
                var filePath = string.Format("{0}\\{1}.json", Environment.CurrentDirectory, offerName);
                Console.WriteLine("Writing response to JSON file " + filePath);
                using (var writer = new StreamWriter(filePath)) {
                    writer.WriteLine(rateCardResponse);
                }
                // Convert the Stream to a strongly typed RateCardPayload object.
                // You can also walk through this object to manipulate the individuals member objects.
                RateCardPayload payload = JsonConvert.DeserializeObject <RateCardPayload>(rateCardResponse);
                // Console.WriteLine(rateCardResponse.ToString());
                filePath = string.Format("{0}\\{1}.csv", Environment.CurrentDirectory, offerName);
                Console.WriteLine("Writing response to CSV file " + filePath);
                using (var writeStream = new StreamWriter(filePath)) {
                    payload.ToCSV(writeStream);
                }
            }
            response.Close();
            receiveStream.Close();
        }