-
Notifications
You must be signed in to change notification settings - Fork 8
Typical setup
Since you typically want to be able to use Web Anchor while developing, testing and also going live, you need to be able to configure it differently in these situations. What I present here is how I've been doing it. If you have better ideas on how to setup Web Anchor, please share!
While developing, you don't want to make any api calls at all. This is where you need to be able to create a fake api. You can do this by just implementing your interface. Look at this.
[BaseLocation("posts")]
public interface ITypicodeApi
{
[Get("")]
Task<List<Post>> GetPosts();
}
public class FakeApi : ITypicodeApi
{
public async Task<List<Post>> GetPosts()
{
return new List<Post>
{
new Post(){ UserId = 1, Id = 1, Title = "Title", Body = "Body"}
};
}
}
And in your IoC-setup, in this case structure map, you need to have a switch to decide when to use the FakeApi. IsDevelop could be provided through the app.config, web.config or maybe a parameter to the executable, you decide. I usually have some value in the app.config or web.config, and use transformations on this config to change it when deploying to test or live environment.
For<ITypicodeApi>().Use(x =>
{
if(isDevelop)
{
return new FakeApi();
}
else
{
return Api.For<ITypicodeApi>("http://jsonplaceholder.typicode.com");
}
});
Continuing on last example, when you want to deploy your code to some test-server or something, you probably don't want to make calls to the real http-resource. Just replace the host with some variable, that is retrieved from your configuration file as well, and make sure that this variable is updated though configuration transformations.
For<ITypicodeApi>().Use(x =>
{
if(isDevelop)
{
return new FakeApi();
}
else
{
return Api.For<ITypicodeApi>(typicodehost);
}
});
For<ITypicodeApi>().Use(x =>
{
if(isDevelop)
{
return A.Fake<ITypicodeApi>();
}
else
{
return Api.For<ITypicodeApi>(typicodehost);
}
});
- Getting started
- Basics (Http GET & Base Location)
- Basics pt. 2 (POST/PUT/DELETE, HttpResponseMessage, Exceptions, HttpClient, IDisposable)
- Building an HttpRequestMessage
- Parameter attributes
- Handling responses
- ApiSettings
- Typical setup (Develop/Test/Production & Mocking)
- Logging
- Testing
- Generic Api
- Multipart