Skip to content

jovemnf/Jovemnf.WebAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jovemnf.WebAPI

Pacode .net core para usar os verbos HTTP.

Modo de Usar

GET

Para fazer um Get Simples segue o exemplo abaixo

using System;
using Jovemnf.WebAPI;

namespace TesteWebAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            Exec();
            Console.Read();
        }

        private static async void Exec() {
            string url = "https://jsonplaceholder.typicode.com/posts/1";
            using (WebAPI web = new WebAPI(url, 5000, WebAPI.MethodRequest.GET))
            {
                try {
                    var post = await web.SendAndGet<Post>();
                    Console.WriteLine("ID: " + post.id);
                    Console.WriteLine("USER-ID: " + post.userId);
                    Console.WriteLine("TITLE: " + post.title);
                    Console.WriteLine("BODY: " + post.body);
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }
    }

    class Post {
        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }
}

GET LIST

Buscando uma listagem de posts

using System;
using System.Collections.Generic;
using Jovemnf.WebAPI;

namespace TesteWebAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            Exec();
            Console.Read();
        }

        private static async void Exec() {
            string url = "https://jsonplaceholder.typicode.com/posts";
            using (WebAPI web = new WebAPI(url, 5000, WebAPI.MethodRequest.GET))
            {
                try {
                    var posts = await web.SendAndGet<List<Post>>();
                    Console.WriteLine("COUNT: " + posts.Count);
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }
    }

    class Post {
        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }
}

POST

Utilizando a lib via POST e aguardando resultado

using System;
using Jovemnf.WebAPI;

namespace TesteWebAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            var post = new Post
            {
                userId = 1,
                title = "titulo 1",
                body = "body 1"
            };

            Exec(post);
            Console.Read();
        }

        private static async void Exec(Post post) {
            string url = "https://jsonplaceholder.typicode.com/posts";
            using (WebAPI web = new WebAPI(url, 5000, WebAPI.MethodRequest.POST))
            {
                try {
                    web.SetJson(post);

                    var returned = await web.Send<Post>();
                    Console.WriteLine("ID: " + returned.id);
                    Console.WriteLine("USER-ID: " + returned.userId);
                    Console.WriteLine("TITLE: " + returned.title);
                    Console.WriteLine("BODY: " + returned.body);
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }
    }

    class Post {
        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }
}

Utilizando a lib via POST sem nenhum resultado

using System;
using Jovemnf.WebAPI;

namespace TesteWebAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            var post = new Post
            {
                userId = 1,
                title = "titulo 1",
                body = "body 1"
            };

            Exec(post);
            Console.Read();
        }

        private static async void Exec(Post post) {
            string url = "https://jsonplaceholder.typicode.com/posts";
            using (WebAPI web = new WebAPI(url, 5000, WebAPI.MethodRequest.POST))
            {
                try {
                    web.SetJson(post);
                    await web.Send();
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }
    }

    class Post {
        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }
}

Using header

string url = "https://jsonplaceholder.typicode.com/posts";
using (WebAPI web = new WebAPI(url, 5000, WebAPI.MethodRequest.POST))
{
    try {
        web.SetHeader("Bearer", "JWT 563563563563563563564564");
        web.SetJson(post);
    } catch (Exception e) {
        Console.WriteLine(e);
    }
}

About

Pacode .net core para usar os verbos HTTP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages