public static async Task <ItemListResult> FetchItemListAsync(string categoryId, int page = 1, int limit = 50)
        {
            string ttbKey    = Environment.GetEnvironmentVariable("ALADIN_TTB_KEY");
            string partnerId = Environment.GetEnvironmentVariable("ALADIN_PARTNER_ID");

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

            queryDict.Add("querytype", "itemnewall");
            queryDict.Add("version", "20131101");
            queryDict.Add("cover", "big");
            queryDict.Add("output", "js");
            queryDict.Add("start", page.ToString());
            queryDict.Add("maxresults", limit.ToString());
            queryDict.Add("categoryid", categoryId);
            queryDict.Add("ttbkey", ttbKey);
            queryDict.Add("partner", partnerId);

            StringBuilder sb = new StringBuilder(256);

            sb.Append(Aladin.Const.Endpoint.List + "?");
            foreach (var kvp in queryDict)
            {
                sb.Append(kvp.Key).Append("=").Append(kvp.Value).Append("&");
            }

            string response = await httpClient.GetStringAsync(sb.ToString());

            ItemListResult result = JsonConvert.DeserializeObject <ItemListResult>(response);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets collection of gallery items.
        /// </summary>
        /// <param name='parameters'>
        /// Optional. Query parameters. If null is passed returns all gallery
        /// items.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List of gallery items.
        /// </returns>
        public async Task <ItemListResult> ListAsync(ItemListParameters parameters, CancellationToken cancellationToken)
        {
            // Validate

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("parameters", parameters);
                Tracing.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url          = "/Microsoft.Gallery/galleryitems?";
            bool   appendFilter = true;

            if (parameters != null && parameters.Filter != null)
            {
                appendFilter = false;
                url          = url + "$filter=" + Uri.EscapeDataString(parameters.Filter != null ? parameters.Filter.Trim() : "");
            }
            if (parameters != null && parameters.Top != null)
            {
                url = url + "&$top=" + Uri.EscapeDataString(parameters.Top.Value.ToString());
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    ItemListResult result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new ItemListResult();
                    JToken responseDoc = null;
                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        responseDoc = JToken.Parse(responseContent);
                    }

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        JToken itemsArray = responseDoc;
                        if (itemsArray != null && itemsArray.Type != JTokenType.Null)
                        {
                            result.Items = new List <GalleryItem>();
                            foreach (JToken itemsValue in ((JArray)itemsArray))
                            {
                                GalleryItem galleryItemInstance = new GalleryItem();
                                result.Items.Add(galleryItemInstance);

                                JToken identityValue = itemsValue["identity"];
                                if (identityValue != null && identityValue.Type != JTokenType.Null)
                                {
                                    string identityInstance = ((string)identityValue);
                                    galleryItemInstance.Identity = identityInstance;
                                }

                                JToken itemNameValue = itemsValue["itemName"];
                                if (itemNameValue != null && itemNameValue.Type != JTokenType.Null)
                                {
                                    string itemNameInstance = ((string)itemNameValue);
                                    galleryItemInstance.Name = itemNameInstance;
                                }

                                JToken itemDisplayNameValue = itemsValue["itemDisplayName"];
                                if (itemDisplayNameValue != null && itemDisplayNameValue.Type != JTokenType.Null)
                                {
                                    string itemDisplayNameInstance = ((string)itemDisplayNameValue);
                                    galleryItemInstance.DisplayName = itemDisplayNameInstance;
                                }

                                JToken publisherValue = itemsValue["publisher"];
                                if (publisherValue != null && publisherValue.Type != JTokenType.Null)
                                {
                                    string publisherInstance = ((string)publisherValue);
                                    galleryItemInstance.Publisher = publisherInstance;
                                }

                                JToken publisherDisplayNameValue = itemsValue["publisherDisplayName"];
                                if (publisherDisplayNameValue != null && publisherDisplayNameValue.Type != JTokenType.Null)
                                {
                                    string publisherDisplayNameInstance = ((string)publisherDisplayNameValue);
                                    galleryItemInstance.PublisherDisplayName = publisherDisplayNameInstance;
                                }

                                JToken versionValue = itemsValue["version"];
                                if (versionValue != null && versionValue.Type != JTokenType.Null)
                                {
                                    string versionInstance = ((string)versionValue);
                                    galleryItemInstance.Version = versionInstance;
                                }

                                JToken summaryValue = itemsValue["summary"];
                                if (summaryValue != null && summaryValue.Type != JTokenType.Null)
                                {
                                    string summaryInstance = ((string)summaryValue);
                                    galleryItemInstance.Summary = summaryInstance;
                                }

                                JToken descriptionValue = itemsValue["description"];
                                if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                {
                                    string descriptionInstance = ((string)descriptionValue);
                                    galleryItemInstance.Description = descriptionInstance;
                                }

                                JToken resourceGroupNameValue = itemsValue["resourceGroupName"];
                                if (resourceGroupNameValue != null && resourceGroupNameValue.Type != JTokenType.Null)
                                {
                                    string resourceGroupNameInstance = ((string)resourceGroupNameValue);
                                    galleryItemInstance.ResourceGroupName = resourceGroupNameInstance;
                                }

                                JToken definitionTemplatesValue = itemsValue["definitionTemplates"];
                                if (definitionTemplatesValue != null && definitionTemplatesValue.Type != JTokenType.Null)
                                {
                                    DefinitionTemplates definitionTemplatesInstance = new DefinitionTemplates();
                                    galleryItemInstance.DefinitionTemplates = definitionTemplatesInstance;

                                    JToken uiDefinitionFileUrlValue = definitionTemplatesValue["uiDefinitionFileUrl"];
                                    if (uiDefinitionFileUrlValue != null && uiDefinitionFileUrlValue.Type != JTokenType.Null)
                                    {
                                        string uiDefinitionFileUrlInstance = ((string)uiDefinitionFileUrlValue);
                                        definitionTemplatesInstance.UiDefinitionFileUrl = uiDefinitionFileUrlInstance;
                                    }

                                    JToken defaultDeploymentTemplateIdValue = definitionTemplatesValue["defaultDeploymentTemplateId"];
                                    if (defaultDeploymentTemplateIdValue != null && defaultDeploymentTemplateIdValue.Type != JTokenType.Null)
                                    {
                                        string defaultDeploymentTemplateIdInstance = ((string)defaultDeploymentTemplateIdValue);
                                        definitionTemplatesInstance.DefaultDeploymentTemplateId = defaultDeploymentTemplateIdInstance;
                                    }

                                    JToken deploymentTemplateFileUrlsSequenceElement = ((JToken)definitionTemplatesValue["deploymentTemplateFileUrls"]);
                                    if (deploymentTemplateFileUrlsSequenceElement != null && deploymentTemplateFileUrlsSequenceElement.Type != JTokenType.Null)
                                    {
                                        definitionTemplatesInstance.DeploymentTemplateFileUrls = new Dictionary <string, string>();
                                        foreach (JProperty property in deploymentTemplateFileUrlsSequenceElement)
                                        {
                                            string deploymentTemplateFileUrlsKey   = ((string)property.Name);
                                            string deploymentTemplateFileUrlsValue = ((string)property.Value);
                                            definitionTemplatesInstance.DeploymentTemplateFileUrls.Add(deploymentTemplateFileUrlsKey, deploymentTemplateFileUrlsValue);
                                        }
                                    }
                                }

                                JToken categoryIdsArray = itemsValue["categoryIds"];
                                if (categoryIdsArray != null && categoryIdsArray.Type != JTokenType.Null)
                                {
                                    galleryItemInstance.CategoryIds = new List <string>();
                                    foreach (JToken categoryIdsValue in ((JArray)categoryIdsArray))
                                    {
                                        galleryItemInstance.CategoryIds.Add(((string)categoryIdsValue));
                                    }
                                }

                                JToken screenshotUrlsArray = itemsValue["screenshotUrls"];
                                if (screenshotUrlsArray != null && screenshotUrlsArray.Type != JTokenType.Null)
                                {
                                    galleryItemInstance.ScreenshotUrls = new List <string>();
                                    foreach (JToken screenshotUrlsValue in ((JArray)screenshotUrlsArray))
                                    {
                                        galleryItemInstance.ScreenshotUrls.Add(((string)screenshotUrlsValue));
                                    }
                                }

                                JToken iconFileUrlsSequenceElement = ((JToken)itemsValue["iconFileUrls"]);
                                if (iconFileUrlsSequenceElement != null && iconFileUrlsSequenceElement.Type != JTokenType.Null)
                                {
                                    galleryItemInstance.IconFileUrls = new Dictionary <string, string>();
                                    foreach (JProperty property2 in iconFileUrlsSequenceElement)
                                    {
                                        string iconFileUrlsKey   = ((string)property2.Name);
                                        string iconFileUrlsValue = ((string)property2.Value);
                                        galleryItemInstance.IconFileUrls.Add(iconFileUrlsKey, iconFileUrlsValue);
                                    }
                                }
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }