Search code examples
swaggerrestsharpambari

How to use RestSharp with Ambari Swagger


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();

Solution

  • 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.