将httpclient通用代码转换为retsharp
我正在尝试将httpclient
代码转换为RestSharp
,但没有成功。我的尝试在下面。
- 我不确定httpclient的
request.content
可以翻译成request.addstringbody
- 在此行
response> wendment> revestion> response.headers.todictionary(a) ; a.key,a =>
private async Task<T> SendAsync<T>(string requestUri, Method method, object? content = null)
{
var request = new RestRequest($"{_baseUrl}{requestUri}", method);
if (content is not null)
{
request.AddStringBody(JsonSerializer.Serialize(content), DataFormat.Json);
}
if (!string.IsNullOrWhiteSpace(_apiKey))
{
request.AddHeader("X-MBX-APIKEY", _apiKey);
}
var response = await _restClient.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var jsonString = response.Content;
try
{
var data = JsonSerializer.Deserialize<T>(jsonString!);
return data!;
}
catch (JsonException ex)
{
var clientException = new BinanceClientException($"Failed to map server response from '${requestUri}' to given type", -1, ex)
{
StatusCode = (int)response.StatusCode,
Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value)
};
throw clientException;
}
}
BinanceHttpException? httpException;
var contentString = response.Content;
var statusCode = (int)response.StatusCode;
if (statusCode is >= 400 and < 500)
{
if (string.IsNullOrWhiteSpace(contentString))
{
httpException = new BinanceClientException("Unsuccessful response with no content", -1);
}
else
{
try
{
httpException = JsonSerializer.Deserialize<BinanceClientException>(contentString);
}
catch (JsonException ex)
{
httpException = new BinanceClientException(contentString, -1, ex);
}
}
}
else
{
httpException = new BinanceServerException(contentString!);
}
httpException.StatusCode = statusCode;
httpException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw httpException;
}
=& gt
private async Task<T> SendAsync<T>(string requestUri, HttpMethod httpMethod, object content = null)
{
using (var request = new HttpRequestMessage(httpMethod, this.baseUrl + requestUri))
{
if (!(content is null))
{
request.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
}
if (!(this.apiKey is null))
{
request.Headers.Add("X-MBX-APIKEY", this.apiKey);
}
HttpResponseMessage response = await this.httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
using (HttpContent responseContent = response.Content)
{
string jsonString = await responseContent.ReadAsStringAsync();
if (typeof(T) == typeof(string))
{
return (T)(object)jsonString;
}
else
{
try
{
T data = JsonConvert.DeserializeObject<T>(jsonString);
return data;
}
catch (JsonReaderException ex)
{
var clientException = new BinanceClientException($"Failed to map server response from '${requestUri}' to given type", -1, ex);
clientException.StatusCode = (int)response.StatusCode;
clientException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw clientException;
}
}
}
}
else
{
using (HttpContent responseContent = response.Content)
{
BinanceHttpException httpException = null;
string contentString = await responseContent.ReadAsStringAsync();
int statusCode = (int)response.StatusCode;
if (400 <= statusCode && statusCode < 500)
{
if (string.IsNullOrWhiteSpace(contentString))
{
httpException = new BinanceClientException("Unsuccessful response with no content", -1);
}
else
{
try
{
httpException = JsonConvert.DeserializeObject<BinanceClientException>(contentString);
}
catch (JsonReaderException ex)
{
httpException = new BinanceClientException(contentString, -1, ex);
}
}
}
else
{
httpException = new BinanceServerException(contentString);
}
httpException.StatusCode = statusCode;
httpException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw httpException;
}
}
}
}
I'm trying to convert the HttpClient
code to RestSharp
but unsuccessfully. My attempt is below.
- I'm not sure whether HttpClient's
request.Content
can be translated torequest.AddStringBody
- There is a compile time error at this line
response.Headers.ToDictionary(a => a.Key, a => a.Value)
private async Task<T> SendAsync<T>(string requestUri, Method method, object? content = null)
{
var request = new RestRequest(quot;{_baseUrl}{requestUri}", method);
if (content is not null)
{
request.AddStringBody(JsonSerializer.Serialize(content), DataFormat.Json);
}
if (!string.IsNullOrWhiteSpace(_apiKey))
{
request.AddHeader("X-MBX-APIKEY", _apiKey);
}
var response = await _restClient.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var jsonString = response.Content;
try
{
var data = JsonSerializer.Deserialize<T>(jsonString!);
return data!;
}
catch (JsonException ex)
{
var clientException = new BinanceClientException(quot;Failed to map server response from '${requestUri}' to given type", -1, ex)
{
StatusCode = (int)response.StatusCode,
Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value)
};
throw clientException;
}
}
BinanceHttpException? httpException;
var contentString = response.Content;
var statusCode = (int)response.StatusCode;
if (statusCode is >= 400 and < 500)
{
if (string.IsNullOrWhiteSpace(contentString))
{
httpException = new BinanceClientException("Unsuccessful response with no content", -1);
}
else
{
try
{
httpException = JsonSerializer.Deserialize<BinanceClientException>(contentString);
}
catch (JsonException ex)
{
httpException = new BinanceClientException(contentString, -1, ex);
}
}
}
else
{
httpException = new BinanceServerException(contentString!);
}
httpException.StatusCode = statusCode;
httpException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw httpException;
}
HttpClient
private async Task<T> SendAsync<T>(string requestUri, HttpMethod httpMethod, object content = null)
{
using (var request = new HttpRequestMessage(httpMethod, this.baseUrl + requestUri))
{
if (!(content is null))
{
request.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
}
if (!(this.apiKey is null))
{
request.Headers.Add("X-MBX-APIKEY", this.apiKey);
}
HttpResponseMessage response = await this.httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
using (HttpContent responseContent = response.Content)
{
string jsonString = await responseContent.ReadAsStringAsync();
if (typeof(T) == typeof(string))
{
return (T)(object)jsonString;
}
else
{
try
{
T data = JsonConvert.DeserializeObject<T>(jsonString);
return data;
}
catch (JsonReaderException ex)
{
var clientException = new BinanceClientException(quot;Failed to map server response from '${requestUri}' to given type", -1, ex);
clientException.StatusCode = (int)response.StatusCode;
clientException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw clientException;
}
}
}
}
else
{
using (HttpContent responseContent = response.Content)
{
BinanceHttpException httpException = null;
string contentString = await responseContent.ReadAsStringAsync();
int statusCode = (int)response.StatusCode;
if (400 <= statusCode && statusCode < 500)
{
if (string.IsNullOrWhiteSpace(contentString))
{
httpException = new BinanceClientException("Unsuccessful response with no content", -1);
}
else
{
try
{
httpException = JsonConvert.DeserializeObject<BinanceClientException>(contentString);
}
catch (JsonReaderException ex)
{
httpException = new BinanceClientException(contentString, -1, ex);
}
}
}
else
{
httpException = new BinanceServerException(contentString);
}
httpException.StatusCode = statusCode;
httpException.Headers = response.Headers.ToDictionary(a => a.Key, a => a.Value);
throw httpException;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您是否检查过
HeaderParameter
的属性是什么?它是名称
,而不是键
。此外,您还有大量冗余代码:Well, have you checked what are the properties of
HeaderParameter
are? It'sName
, notKey
. In addition, you have loads of redundant code: