问题描述
我正在尝试使用RestSharp包装Plivo API(是的,我知道它已经完成了).
I am attempting to wrap the Plivo API (yes I'm aware it's been done) using RestSharp.
但是,我找不到将API命名约定转换为我自己的方法,例如:
However, I cannot find a method to translate the API Naming conventions to my own, for example:
呼叫"( https://www. plivo.com/docs/api/call/#make-an-outbound-call )至少需要满足以下条件:
A "Call` (https://www.plivo.com/docs/api/call/#make-an-outbound-call) requires a minimum of:
to
,from
和answer_url
参数.
这些参数也区分大小写.
These parameters are also case-sensitive.
我希望能够提供一个CallRequest类,包装我的首选命名约定中所需的数据,然后以某种方式转换它们,再由RestSharp进行序列化.
I would like to be able to provide a CallRequest Class, wrapping the data required in my preferred naming conventions, and then somehow translate these prior to serialization by RestSharp.
示例:
public class CallRequest
{
/// <summary>
/// The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code.
/// </summary>
public string From { get; set; }
/// <summary>
/// The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:[email protected]. To make bulk calls, the delimiter < is used. For eg. 15677654321<15673464321<sip:[email protected] Yes, you can mix regular numbers and sip endpoints.
/// </summary>
public string To { get; set; }
/// <summary>
/// The URL invoked by Plivo when the outbound call is answered.
/// </summary>
public string AnswerUrl { get; set; }
}
然后将这些数据转换为以下功能中的Plivo约定:
This data would then be translated to Plivo's convention in the following functions:
private T Execute<T>(IRestRequest request) where T : new()
{
var client = new RestClient
{
BaseUrl = new Uri(BaseUrl),
Authenticator = new HttpBasicAuthenticator(_accountId, _authToken),
UserAgent = "PlivoSharp"
};
request.AddHeader("Content-Type", "application/json");
request.AddParameter("auth_id", _accountId, ParameterType.UrlSegment);
request.RequestFormat = DataFormat.Json;
client.AddHandler("application/json", new JsonDeserializer());
var response = client.Execute<T>(request);
if (response.ErrorException == null) return response.Data;
const string message = "Error retrieving response. Check inner details for more info.";
var plivoException = new ApplicationException(message, response.ErrorException);
throw plivoException;
}
public CallResponse MakeCall(CallRequest callRequest)
{
var request = new RestRequest
{
RequestFormat = DataFormat.Json,
Resource = "Account/{auth_id}/Call/",
Method = Method.POST
};
//SOMEHOW TRANSLATE THE PROPERTIES INTO THE DATA BELOW
request.AddBody(new
{
to = "17#####",
from = "18#####",
answer_url = "http://m------.xml"
});
return Execute<CallResponse>(request);
}
推荐答案
不幸的是,看起来好像没有在RestSharp中实现JSON属性重命名.您有两种选择:
Unfortunately it looks as though JSON property renaming is not implemented out of the box in RestSharp. You have a couple of options:
-
从 https://github.com/restsharp/RestSharp 下载Restsharp并重建它自己启用编译器选项
SIMPLE_JSON_DATACONTRACT
.然后,您将能够使用数据协定属性来重命名属性.有关更多信息,请参见此处: RestSharp JsonDeserializer,标识符中带有特殊字符
Download Restsharp from https://github.com/restsharp/RestSharp and rebuild it yourself enabling the compiler option
SIMPLE_JSON_DATACONTRACT
. Then you will be able to rename properties using data contract attributes. For more, see here: RestSharp JsonDeserializer with special characters in identifiers
我刚刚重建了RestSharp的最新版本(版本105.1.0)启用此选项.使用您的课程的以下版本:
I just rebuilt the most recent version of RestSharp (version 105.1.0)with this option enabled. Using the following version of your class:
[DataContract]
public class CallRequest
{
[DataMember(Name = "from")]
public string From { get; set; }
[DataMember(Name = "to")]
public string To { get; set; }
[DataMember(Name = "answer_url")]
public string AnswerUrl { get; set; }
}
我能够生成以下JSON:
I was able to generate the following JSON:
var request = new CallRequest { AnswerUrl = "AnswerUrl", From = "from", To = "to" };
var json = SimpleJson.SerializeObject(request);
Debug.WriteLine(json);
// Prints {"from":"from","to":"to","answer_url":"AnswerUrl"}
我不确定该选项是否经过全面测试,因为它是默认情况下编译出来的.
I'm not sure how thoroughly tested this option is, however, since it's compiled out by default.
使用支持属性重命名的其他序列化程序(例如Json.NET)手动进行序列化和反序列化.为此,请参见 RestSharp-使用Json.net序列化程序(存档此处.)
Manually serialize and deserialize with a different serializer such as Json.NET that supports property renaming. To do this, see RestSharp - using the Json.net serializer (archived here.)
这篇关于RestSharp序列化/反序列化命名转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!