-
Notifications
You must be signed in to change notification settings - Fork 38
More Examples
zijianhuang edited this page Apr 8, 2017
·
3 revisions
Server side:
[Authorize](Authorize)
[RoutePrefix("api/Contacts")](RoutePrefix(_api_Contacts_))
public class ContactsManagementController : ApiController
{
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns>Null if not found.</returns>
[HttpGet](HttpGet)
[Route("People")](Route(_People_))
public Person GetPerson(long id)
{
//...
}
[HttpPost](HttpPost)
[Route("People")](Route(_People_))
public long CreatePerson(Fonlow.Oc.Person person)
{
//...
}
[HttpDelete](HttpDelete)
[Route("People")](Route(_People_))
public void DeleteEntity(long id)
{
//...
}
/// <summary>
///
/// </summary>
/// <returns>0 if not found</returns>
[HttpGet](HttpGet)
[Route("OwnerId")](Route(_OwnerId_))
public long GetOwnerId()
{
//...
}
[HttpGet](HttpGet)
[Route("Companies")](Route(_Companies_))
public Company GetCompany(long id)
{
//...
}
[HttpPost](HttpPost)
[Route("Companies")](Route(_Companies_))
public long CreateCompany(Company company)
{
//...
}
}
[Authorize(Roles = RoleConstants.Admin)](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](HttpPost)
public string Post([FromBody](FromBody)string value)
{
return value.ToUpper();
}
// PUT api/values/5
public void Put(int id, [FromBody](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")](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.Diagnostics.DebuggerStepThroughAttribute())
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")](System.CodeDom.Compiler.GeneratedCodeAttribute(_System.Runtime.Serialization_,-_4.0.0.0_))
[System.Runtime.Serialization.DataContractAttribute(Name="Constants", Namespace="http://fonlow.com/DemoData/2014/02")](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.Diagnostics.DebuggerStepThroughAttribute())
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")](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.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.Person)))
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DemoWebApi.DemoData.Client.Company))](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()](System.Runtime.Serialization.DataMemberAttribute())
public DemoWebApi.DemoData.Client.Address[]() Addresses
{
get
{
return this.AddressesField;
}
set
{
this.AddressesField = value;
}
}
...
Client codes:
[Collection("IISExpressAndDemoOwner")](Collection(_IISExpressAndDemoOwner_))
public class ContactsManagementApiIntegration : IClassFixture<DemoOwnerHttpClientFixture>
{
public ContactsManagementApiIntegration(DemoOwnerHttpClientFixture httpClientFixture)
{
baseUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["Testing_BaseUrl"](_Testing_BaseUrl_));
authorizedClient = httpClientFixture.AuthorizedClient;
contactsApi = new ContactsManagement(authorizedClient, baseUri);
}
Uri baseUri;
HttpClient authorizedClient;
ContactsManagement contactsApi;
EntitySearch searchApi;
[Fact](Fact)
public void TestGetPersonWithInvalidPersonIdThrow400()
{
var e = Assert.Throws<HttpRequestException>(() => contactsApi.GetPerson(123456789));
Assert.Contains("400", e.Message);
}
[Fact](Fact)
public void TestDeleteInvalidPersonIdRaiseException()
{
var e = Assert.Throws<System.Net.Http.HttpRequestException>(() =>
contactsApi.DeleteEntity(12345678));
Assert.Contains("400", e.Message);
}
[Fact](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);
}