Skip to content

Latest commit

 

History

History
210 lines (166 loc) · 7.26 KB

document.md

File metadata and controls

210 lines (166 loc) · 7.26 KB

Logo

Twileloop.JetAPI

[BETA Pre-Release]

Simple | Fluent | Fast

About

An easy to use conveinent fluent syntaxed HTTPClient for all your personal or commercial .NET apps. Do API calls with ease

License

Twileloop.JetAPI is licensed under the MIT License. See the LICENSE file for more details.

This library is absolutely free. If it gives you a smile, A small coffee would be a great way to support my work. Thank you for considering it!

"Buy Me A Coffee"

Simple GET

var response = await new JetRequest<dynamic>()
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/1");

GET 🠚 With Query Params

var response = await new JetRequest<dynamic>()
                         .WithQueries(
                             new Param("postId", 2),
                             new Param("date", "1996-10-28")
                         )
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/comments");```

GET 🠚 With Headers

var response = await new JetRequest<dynamic>()
                         .WithQueries(
                             new Param("postId", 3)
                         )
                         .WithHeaders(
                             new Param("x-request-by", "name"),
                             new Param("x-limit", 150)
                         )
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/comments");                         .ExecuteAsync("https://jsonplaceholder.typicode.com/comments");```

POST 🠚 With JSON String

var jsonString = @"{""title"":""Foo"",""bar"":""Bar"",""userid"":1}";

var response = await new JetRequest<dynamic>()
                        .Post()
                        .WithBody(
                            new RawBody(ContentType.Json, jsonString)
                        )
                        .ExecuteAsync("https://jsonplaceholder.typicode.com/posts");

PUT 🠚 With Object As JSON

var instance = new {
    Title = "Foo",
    Bar = "Bar",
    UserId = 1
};

var response = await new JetRequest<MyResponseModel>()
                        .Put()
                        .WithBody(
                            new RawBody(ContentType.Json, instance)
                        )
                        .ExecuteAsync("https://jsonplaceholder.typicode.com/posts");

GET 🠚 With Basic-Authentication

var response = await new JetRequest<MyResponseModel>()
                         .Get()
                         .WithAuthentication(new BasicAuthentication {
                             Username = "username",
                             Password = "password"
                         })
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/5");

GET 🠚 With JWT Bearer-Authentication

var response = await new JetRequest<MyResponseModel>()
                         .Get()
                         .WithAuthentication(new BearerToken("<BEARER_TOKEN>"))
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/5");

GET 🠚 With API_KEY Authentication

var response = await new JetRequest<MyResponseModel>()
                         .Get()
                         .WithAuthentication(new ApiKey("Api-Key", "<API_KEY>"))
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/5");

PATCH 🠚 And Handle Exceptions Yourself

var response = await new JetRequest<MyResponseModel>()
                         .Patch()
                         .HandleExceptions(
                             ex => Console.WriteLine($"An exception occured. Message: {ex.Message}");
                         )
                         .ExecuteAsync("htt://jsonplaceholder.typicode.com/posts/5");

GET 🠚 With Success/Failure Captures

var response = await new JetRequest<MyResponseModel>()
                         .Get()
                         .WithCaptures(
                             successResponse => Console.WriteLine("Success");,
                             failureResponse => Console.WriteLine("Failure");
                         )
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/5");

PUT 🠚 With Custom Captures Based On HTTP StatusCode

var response = await new JetRequest<MyResponseModel>()
                         .Put()
                         .WithCaptures(
                             (HttpStatusCode.OK, () => Console.WriteLine("Ok")),
                             (HttpStatusCode.NotFound, () => Console.WriteLine("Not Found")),
                             (HttpStatusCode.Unauthorized, () => Console.WriteLine("UnAuthorized")),
                             (HttpStatusCode.Forbidden, () => Console.WriteLine("Forbidden"))
                         )
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/fake");

GET 🠚 As JSON/XML/HTML or TEXT

var response = await new JetRequest<MyResponseModel>()
                          .Get()
                          .FetchAs(ContentType.XML)
                          .ExecuteAsync("https://samplexml.com/auth/demoxml.xml");

GET 🠚 And Pass Request Cookies

var response = await new JetRequest<MyResponseModel>()
                          .WithCookies(
                              new Param("Cookie1", "<CookieValue1>"),
                              new Param("Cookie2", "<CookieValue2>")
                           )
                          .FetchAs(ContentType.HTML)
                          .ExecuteAsync("https://google.com");

Listen To Events With Interceptors

Create your own intercepter by inheriting from Interceptor base class

public class CustomInterceptor : Interceptor {

        public override void OnInit() {
            Console.WriteLine("Started...");
            base.OnInit();
        }

        public override void OnRequesting(Request request) {
            Console.WriteLine("Let's modify request from interceptor");
            request.HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");

            Console.WriteLine("Enough. Now start requesting...");
            base.OnRequesting(request);
        }

        public override void OnResponseReceived() {
            Console.WriteLine("Got response...");
            base.OnResponseReceived();
        }

    }

Do your stuff above. Alter anything before request goes or log result after response came etc.. Now let's attach this interceptor to JetAPI

var interceptor = new CustomInterceptor();

 var response = await new JetRequest<dynamic>()
                         .Post()
                         .WithAuthentication(new BearerToken("<BEARER_TOKEN>"))
                         .WithInterceptor(interceptor)
                         .ExecuteAsync("https://jsonplaceholder.typicode.com/posts/5");