4

RestSharp を使用して Plivo API をラップしようとしています (はい、それが行われたことは承知しています)。

ただし、API 命名規則を独自のものに変換する方法が見つかりません。たとえば、次のようになります。

"Call` ( https://www.plivo.com/docs/api/call/#make-an-outbound-call ) には、少なくとも次のものが必要です。

tofrom、およびanswer_urlパラメータが提供されます。

これらのパラメータも大文字と小文字が区別されます。

CallRequest クラスを提供し、好みの命名規則で必要なデータをラップしてから、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:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For eg. 15677654321<15673464321<sip:john1234@phone.plivo.com 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 の規則に変換されます。

    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);
    }
4

1 に答える 1

3

残念ながら、JSON プロパティの名前変更は、RestSharp ではそのままでは実装されていないようです。いくつかのオプションがあります。

  1. https://github.com/restsharp/RestSharpから Restsharp をダウンロードし、コンパイラ オプションを有効にして自分で再構築しますSIMPLE_JSON_DATACONTRACT。その後、データ コントラクト属性を使用してプロパティの名前を変更できます。詳細については、こちらを参照してください:識別子に特殊文字を使用した RestSharp JsonDeserializer

    このオプションを有効にして、RestSharp の最新バージョン (バージョン 105.1.0) を再構築しました。クラスの次のバージョンを使用します。

    [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 を生成できました。

        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"}
    

    ただし、このオプションはデフォルトでコンパイルされているため、このオプションがどの程度徹底的にテストされているかはわかりません。

  2. プロパティの名前変更をサポートする Json.NET などの別のシリアライザーを使用して、手動でシリアル化および逆シリアル化します。これを行うには、RestSharp - Json.net シリアライザーの使用(アーカイブはこちら) を参照してください。

于 2015-05-04T20:48:40.823 に答える