This project has moved. For the latest updates, please go here.

Examples

Server side:

    [Authorize]
    [RoutePrefix("api/Contacts")]
    public class ContactsManagementController : ApiController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Null if not found.</returns>
        [HttpGet]
        [Route("People")]
        public Person GetPerson(long id)
        {
//...
        }

        [HttpPost]
        [Route("People")]
        public long CreatePerson(Fonlow.Oc.Person person)
        {
            //...
        }

        [HttpDelete]
        [Route("People")]
        public void DeleteEntity(long id)
        {
            //...
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>0 if not found</returns>
        [HttpGet]
        [Route("OwnerId")]
        public long GetOwnerId()
        {
            //...
        }

        [HttpGet]
        [Route("Companies")]
        public Company GetCompany(long id)
        {
            //...
        }

        [HttpPost]
        [Route("Companies")]
        public long CreateCompany(Company company)
        {
            //...
        }
    }


    [Authorize(Roles = RoleConstants.Admin)]
    public class ValuesController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int id, string name)
        {
            return name+id.ToString();
        }

        [HttpPost]
        public string Post([FromBody]string value)
        {
            return value.ToUpper();
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
            System.Diagnostics.Debug.WriteLine("Put " + id.ToString() + value);
        }

    }



Generated client API codes:

    public partial class ContactsManagement
    {
        
        private System.Net.Http.HttpClient client;
        
        private System.Uri baseUri;
        
        public ContactsManagement(System.Net.Http.HttpClient client, System.Uri baseUri)
        {
            this.client = client;
            this.baseUri = baseUri;
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/People?id={id}
        /// </summary>
        /// <returns>Null if not found.</returns>
        public async Task<Fonlow.Oc.Client.Person> GetPersonAsync(long id)
        {
            var template = new System.UriTemplate("api/Contacts/People?id={id}");
            var uriParameters = new System.Collections.Specialized.NameValueCollection();
            uriParameters.Add("id", id.ToString());
            var requestUri = template.BindByName(this.baseUri, uriParameters);
            var responseMessage = await client.GetAsync(requestUri.ToString());
            responseMessage.EnsureSuccessStatusCode();
            var text = await responseMessage.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<Fonlow.Oc.Client.Person>(text);
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/People?id={id}
        /// </summary>
        /// <returns>Null if not found.</returns>
        public Fonlow.Oc.Client.Person GetPerson(long id)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// POST api/Contacts/People
        /// </summary>
        public async Task<System.Int64> CreatePersonAsync(Fonlow.Oc.Client.Person person)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// POST api/Contacts/People
        /// </summary>
        public long CreatePerson(Fonlow.Oc.Client.Person person)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// DELETE api/Contacts/People?id={id}
        /// </summary>
        public async Task DeleteEntityAsync(long id)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// DELETE api/Contacts/People?id={id}
        /// </summary>
        public void DeleteEntity(long id)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/OwnerId
        /// </summary>
        /// <returns>0 if not found</returns>
        public async Task<System.Int64> GetOwnerIdAsync()
        {
            //...
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/OwnerId
        /// </summary>
        /// <returns>0 if not found</returns>
        public long GetOwnerId()
        {
            //...
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/Companies?id={id}
        /// </summary>
        public async Task<Fonlow.Oc.Client.Company> GetCompanyAsync(long id)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// GET api/Contacts/Companies?id={id}
        /// </summary>
        public Fonlow.Oc.Client.Company GetCompany(long id)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// POST api/Contacts/Companies
        /// </summary>
        public async Task<System.Int64> CreateCompanyAsync(Fonlow.Oc.Client.Company company)
        {
            //...
        }
        
        /// <summary>
        /// 
        /// POST api/Contacts/Companies
        /// </summary>
        public long CreateCompany(Fonlow.Oc.Client.Company company)
        {
            //...
        }
    }


    public partial class Values
    {
        
//...
        
        public async Task<IEnumerable<System.String>> GetAsync()
        {
            //...
        }
        
        public System.Collections.Generic.IEnumerable<string> Get()
        {
            //...
        }
        
        public async Task<System.String> GetAsync(int id, string name)
        {
            //...
        }
        
        public string Get(int id, string name)
        {
            //...
        }
        
        public async Task<System.String> PostAsync(string value)
        {
            //...
        }
        
        public string Post(string value)
        {
            //...
        }
        
        public async Task PutAsync(int id, string value)
        {
            //...
        }
        
        public void Put(int id, string value)
        {
            //...
        }

        public async Task DeleteAsync(int id)
        {
            //...
        }
        
        public void Delete(int id)
        {
            //...
        }
    }



The prototypes of clients API functions are logically identical to the prototypes of the Web Api controller functions, and the implementations are generated too so you don't need to write codes of the same design again and again.


Generated client data classes:

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("http://fonlow.com/DemoData/2014/02", ClrNamespace="DemoWebApi.DemoData.Client")]

namespace DemoWebApi.DemoData.Client
{
    using System.Runtime.Serialization;
    
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Constants", Namespace="http://fonlow.com/DemoData/2014/02")]
    public partial class Constants : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Entity", Namespace="http://fonlow.com/DemoData/2014/02")]
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(DemoWebApi.DemoData.Client.Person))]
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(DemoWebApi.DemoData.Client.Company))]
    public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        
        private DemoWebApi.DemoData.Client.Address[] AddressesField;
        
        private long IdField;
        
        private string NameField;
        
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        public DemoWebApi.DemoData.Client.Address[] Addresses
        {
            get
            {
                return this.AddressesField;
            }
            set
            {
                this.AddressesField = value;
            }
        }
...


Client codes:

    [Collection("IISExpressAndDemoOwner")]
    public class ContactsManagementApiIntegration : IClassFixture<DemoOwnerHttpClientFixture>
    {
        public ContactsManagementApiIntegration(DemoOwnerHttpClientFixture httpClientFixture)
        {
            baseUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["Testing_BaseUrl"]);
            authorizedClient = httpClientFixture.AuthorizedClient;
            contactsApi = new ContactsManagement(authorizedClient, baseUri);
        }


        Uri baseUri;

        HttpClient authorizedClient;

        ContactsManagement contactsApi;

        EntitySearch searchApi;

        [Fact]
        public void TestGetPersonWithInvalidPersonIdThrow400()
        {
            var e = Assert.Throws<HttpRequestException>(() => contactsApi.GetPerson(123456789));
            Assert.Contains("400", e.Message);
        }

        [Fact]
        public void TestDeleteInvalidPersonIdRaiseException()
        {
            var e = Assert.Throws<System.Net.Http.HttpRequestException>(() =>
             contactsApi.DeleteEntity(12345678));
            Assert.Contains("400", e.Message);
        }

        [Fact]
        public void TestCreatePersonAndGetThenDelete()
        {
            string name = "Somebody Help" + Guid.NewGuid().ToString("N");

            var person = new Person()
            {
                Name = name,
                GivenName = "Somebody",
                Surname = "Help",
            };
            var id = contactsApi.CreatePerson(person);

            var personRetrieved = contactsApi.GetPerson(id);
            Assert.Equal(name, personRetrieved.Name);
            Assert.Equal(id, personRetrieved.Id);

            contactsApi.DeleteEntity(id);
        }

Last edited Nov 5, 2015 at 7:16 PM by zijianhuang, version 1