How to use RestSharp with Ambari Swagger

隐身守侯 提交于 2021-02-11 12:22:49

问题


How do I connect to Ambari's Swagger interface with a RestSharp client ?

This code works and returns expected json:

        HttpClientHandler handler = new HttpClientHandler
        {
            Credentials = new System.Net.NetworkCredential("xxx", "yyyyy")
        };

        using (var httpClient = new HttpClient(handler))
        {
            var activationUrl = "https://aaaa.azurehdinsight.net";

            var uri = new Uri(activationUrl + "/api/v1/users");
            var response = await httpClient.GetAsync(uri);
            Assert.IsTrue(response.IsSuccessStatusCode);

            var result = await response.Content.ReadAsStringAsync();

        }

This code does not work and returns 406 NotAcceptable:

        var client = new RestSharp.RestClient("https://aaaa.azurehdinsight.net/api/v1/");
        var credentials = new System.Net.NetworkCredential("xxx", "yyyy");
        client.Authenticator = new NtlmAuthenticator(credentials);
        client.DefaultParameters.Clear();

        var request = new RestSharp.RestRequest("users", RestSharp.Method.GET);
        var response = await client.ExecuteAsync<string>(request);

If I tweak the password, it returns Unauthorized, so I know I am authenticating. I think the trick will be to make the RestSharp properties like HttpClient. That is why I removed the Headers with:

 client.DefaultParameters.Clear();

回答1:


I found this to work with RestSharp:

        var client = new RestSharp.RestClient("https://xxxx.azurehdinsight.net/api/v1/");
        var credentials = new System.Net.NetworkCredential("yyyy", "ZZZZzzzz");
        client.Authenticator = new NtlmAuthenticator(credentials);

        //must pass this header
        client.DefaultParameters.Add(new RestSharp.Parameter("X-Requested-By", "my_computer_name", RestSharp.ParameterType.HttpHeader));

        var request = new RestSharp.RestRequest("users", RestSharp.Method.GET);

        //Accept Header cannot be passed
        // "{Accept=application/json, text/json, text/x-json, text/javascript, application/xml, text/xml}"
        request.OnBeforeRequest = (http) =>
        {
            Debug.WriteLine(http.Headers.Count());
            var header = http.Headers.Where(h => h.Name == "Accept");
            http.Headers.Remove(header.First());
            Debug.WriteLine(http.Headers.Count());
        };

        request.RequestFormat = RestSharp.DataFormat.Json;

        var response = await client.ExecuteAsync<string>(request);

        Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.OK);

Other lessons learned:

  • SwaggerHub https://swagger.io/tools/swaggerhub/ creates the most acceptable client from the swagger json.
  • Swagger json is poorly implemented for Ambari. Expect issues no matter the class generator.
  • If you get it to work with one version of Ambari/HDInsight, it may not work with others due to model changes.
  • I ended up using Fiddler to eavesdrop on the Ambari client. I was able to determine the Request and Response json needed to implement what I wanted. I was also able to determine differences in the headers between the client(worked) and RestSharp (had issues). By removing the Header differences I could get it to work. I used putty to get the Swagger json, but like I said earlier, it is poorly defined.


来源:https://stackoverflow.com/questions/63181329/how-to-use-restsharp-with-ambari-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!