Upgrade to Pro — share decks privately, control downloads, hide ads and more …

API Deep Dive

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for EShy EShy
March 16, 2015
36

API Deep Dive

Avatar for EShy

EShy

March 16, 2015

Transcript

  1. A single controller for each resource (Add ApiController with actions)

    public interface IReallyExcitingCrudApi<T, in TKey> where T : class { [Post("")] Task<T> Create([Body] T paylod); [Get("")] Task<List<T>> ReadAll(); [Get("/{key}")] Task<T> ReadOne(TKey key); [Put("/{key}")] Task Update(TKey key, [Body] T payload); [Delete("/{key}")] Task Delete(TKey key); } var api = RestService.For<IReallyExcitingCrudApi<User, int>> (baseUri+"users"); public class UsersController : ApiController { // GET: api/Users public IEnumerable<UserModel> Get() { return new UserModel[] { user1, user2 }; } // GET: api/Users/5 public UserModel Get(int id) { return "value"; } // POST: api/Users public void Post([FromBody] UserModel value) { } // PUT: api/Users/5 public void Put(int id, [FromBody] UserModel value) { } // DELETE: api/Users/5 public void Delete(int id) { } } With ReFit
  2. Each action has an attribute defining it’s relative path [Route("Movies")]

    public List<MovieModel> GetMovies() { var movies = GetMoviesFromDatabase(); return movies.ConvertAll(MovieModel.ToModel); } [Route("Movies/{id}")] public MovieModel GetMovieDetails(int id) { var movie = GetFromDatabase(id); return MovieModel.ToModel(movie); } With ReFit [Get("/Movies")] Task<List<MovieModel>> GetMoviesAsync(); [Get("/Movies/{id}")] Task<MovieModel> GetMovieDetailsAsync(int id);