如何将 RestSharp 与 Ambari Swagger 一起使用 [英] How to use RestSharp with Ambari Swagger
本文介绍了如何将 RestSharp 与 Ambari Swagger 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 RestSharp 客户端连接到 Ambari 的 Swagger 界面?
How do I connect to Ambari's Swagger interface with a RestSharp client ?
此代码有效并返回预期的 json:
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();
}
此代码不起作用并返回 406 NotAcceptable:
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);
如果我调整密码,它会返回未经授权,所以我知道我正在验证.我认为诀窍是制作像 HttpClient 这样的 RestSharp 属性.这就是我删除标题的原因:
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();
推荐答案
我发现这可以与 RestSharp 一起使用:
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);
其他经验教训:
- SwaggerHub https://swagger.io/tools/swaggerhub/ 打造最容易接受的客户端来自大摇大摆的 json.
- Swagger json 对于 Ambari 的实现很差.无论类生成器如何,都会出现问题.
- 如果您让它与 Ambari/HDInsight 的一个版本一起使用,由于模型更改,它可能无法与其他版本一起使用.
- 我最终使用 Fiddler 窃听了 Ambari 客户端.我能够确定实现我想要的所需的请求和响应 json.我还能够确定客户端(工作)和 RestSharp(有问题)之间的标头差异.通过删除标题差异,我可以让它工作.我使用 putty 来获取 Swagger json,但就像我之前所说的,它的定义很差.
- 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.
这篇关于如何将 RestSharp 与 Ambari Swagger 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文