public IActionResult Query([FromQuery(Name = "query")] string query)
 {
     using (var w = new WebClient())
     {
         string url = "https://graphql.contentstack.com/stacks/" +
                      _client.GetApplicationKey() + "?access_token=" +
                      _client.GetAccessToken() + "&environment=" +
                      _client.GetEnvironment() + "&query=" + query;
         return(Content(JObject.Parse(w.DownloadString(url)).ToString()));
     }
 }
Exemplo n.º 2
0
        private void ProcessContentType(ContentstackClient stack, string contentTypeUid, OutputWriter writer)
        {
            //TODO: paging results with threads?
            // https://www.contentstack.com/docs/developers/apis/graphql-content-delivery-api/explorer/?api_key=APIKEY&access_token=DELIVERY_TOKEN&environment=ENVIRONMENT
            string query = "{all_" + contentTypeUid + "{total items{url title system{uid}}}}";

            using (HttpClient http = new HttpClient())
            {
                string request = "https://graphql.contentstack.com/stacks/" +
                                 stack.GetApplicationKey() + "?access_token=" +
                                 stack.GetAccessToken() + "&environment=" +
                                 stack.GetEnvironment() + "contentdelivery&query=" + query;
                int attempt = 0;

                do
                {
                    try
                    {
                        JObject fromGraphQl = JObject.Parse(http.GetStringAsync(request).GetAwaiter().GetResult());
                        int     count       = Int32.Parse(fromGraphQl["data"]["all_" + contentTypeUid]["total"].ToString());

                        foreach (JToken group in fromGraphQl.SelectTokens("$.data.all_" + contentTypeUid + ".items"))
                        {
                            JToken myGroup = group;

                            foreach (var entry in myGroup.Children())
                            {
                                string entryUid = entry.SelectToken("system").SelectToken("uid").ToString();
                                writer.Message(this, $"{entryUid} [{contentTypeUid}] : {entry["title"]} ({entry["url"]})");
                            }
                        }

                        attempt = 3;
                    }
                    catch (Exception ex)
                    {
                        writer.Message(this, ex + " : " + ex.Message);
                    }
                }while (attempt++ < 3);
            }
        }