Created
October 26, 2023 01:40
-
-
Save marianozunino/8f04b7d270d764fa277a25ae0f960982 to your computer and use it in GitHub Desktop.
Consumir REST desde maui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Refit; | |
| namespace MauiApp1; | |
| // Creamos los DTOs necesarios para poder serializar los resultados de la api | |
| public class TodoDto | |
| { | |
| public long Id { get; set; } | |
| public string Title { get; set; } | |
| } | |
| // Declaramos el contrato de nuestra api | |
| public interface ITodoApi | |
| { | |
| [Get("/typicode/demo/posts")] | |
| Task<List<TodoDto>> GetTodos(); | |
| } | |
| public partial class MainPage : ContentPage | |
| { | |
| public MainPage() | |
| { | |
| InitializeComponent(); | |
| } | |
| // Metodo que se ejecuta al momento de renderizar la app | |
| protected override async void OnAppearing() | |
| { | |
| // Creamos un restservice que implemente la interfaz declarada | |
| var api = RestService.For<ITodoApi>("https://my-json-server.typicode.com"); | |
| var todos = await api.GetTodos(); | |
| base.OnAppearing(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment