You can find some examples in the Source Code > Restafari.Demo.Client project


Create your client class and inherit from RestClientBase. Now you can call Get, Post, Put, Delete, GetList, PostList, PutList, DeleteList methods. You also could find the async pattern implemented: BeginPost -> EndRequest or async-await methods: PostAsyc, GetListAsync.

 

public class ChatClient : RestClientBase
{
    private readonly string host;
    private const string MessagesResource = "http://{0}/api/message";
    private const string SubscriptionsResource = "http://{0}/api/subscriptions";

    public ChatClient(string host)
    {
        this.host = host;
    }

    public string CreateSubscription(string clientName, List<string> messageTypes = null)
    {
        var url = string.Format(SubscriptionsResource + "/{1}", this.host, clientName);
        var parameters = new Parameters();
        if (messageTypes != null)
        {
            parameters.Add("messageTypes", messageTypes);
        }

        return this.Post<string>(url, parameters);
    }

    public void DeleteSubscription(string subscriptionId)
    {
        var url = string.Format(SubscriptionsResource + "/{1}", this.host, subscriptionId);
        this.Delete(url);
    }

    public MessagesResult GetMessages(string subscriptionId, int maxNumberOfMessages)
    {
        var url = string.Format(MessagesResource + "/{1}/{2}", this.host, subscriptionId, maxNumberOfMessages);
        return this.Get<MessagesResult>(url);
    }

    public void SendMessage(string parameters, string messageType = null)
    {
        var url = string.Format(MessagesResource, this.host);
        var message = new Message {Parameters = parameters, MessageType = messageType};
        var restParams = new Parameters { { "message", message } };
        this.Post(url, restParams);
    }
}

Last edited Sep 12, 2013 at 11:27 AM by fescolar, version 2

Comments

No comments yet.