Coder Social home page Coder Social logo

httwrap's Introduction

Httwrap

General purpose, simple but useful HttpClient wrapper for .NET & Xamarin/Mono

Build statusCoverage Status

How to use

Install

PM> Install-Package Httwrap

Init

  IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/");
  IHttwrapClient _httwrap = new HttwrapClient(configuration);

GET

Basic

IHttwrapResponse<Product> response = await _httwrap.GetAsync<Product>("api/products/1");
Dump(response.Data);
Dump(response.StatusCode);

With QueryString

It supports DataMember and IgnoreDataMember attributes.

/*
public class FilterRequest
{
  [DataMember(Name = "cat")]
  public string Category { get; set; }
  
  public int NumberOfItems { get; set; }
}
*/

var payload = new FilterRequest
{
  Category = "Shoes",
  NumberOfItems = 10
};

//Url: api/test?cat=Shoes&NumberOfItems=10
IHttwrapResponse<List<Product>> response =
                            await _client.GetAsync<List<Product>>("api/test", payload);

Dump(response.Data);
Dump(response.StatusCode);

Serialize response

IHttwrapResponse response = await _httwrap.GetAsync("api/products");
List<Product> values = response.ResultAs<List<Product>>();
Dump(response.StatusCode);

/* ResultAs<T>() extension method uses Newtonsoft.Json serializer by default.  
To use your own serializer set JExtensions.Serializer = new YourCustomSerializerImpl();*/

POST

Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PostAsync<Product>("api/products",product);
Dump(response.StatusCode);

PUT

Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PutAsync<Product>("api/products/1",product);
Dump(response.StatusCode);

PATCH

Product product = new Product{ Name= "Product A", Quantity = 3};
IHttwrapResponse response = await _httwrap.PatchAsync<Product>("api/products/1",product);
Dump(response.StatusCode);

DELETE

IHttwrapResponse response = await _httwrap.DeleteAsync("api/products/1");
Dump(response.StatusCode);

Error Handler

IHttwrapResponse<List<Product>> response =
      await _httwrap.GetAsync<List<Product>>("api/products", (statusCode, body) =>
      {
        _logger.Error("Body :{0}, StatusCode :{1}", body, statusCode);
      });

Auth

Basic Credentials

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new BasicAuthCredentials("user", "s3cr3t")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

OAuth Credentials

Use existing token

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new OAuthCredentials("token")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

Use Username / password to get token from edpoint

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new OAuthCredentials("us3r", "p4ssw0rd", BaseAddress + "/token")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

Req/Res Interceptor

public class DummyInterceptor : IHttpInterceptor
{
    private readonly IHttwrapClient _client;

    public void OnRequest(HttpRequestMessage request)
    {
    }

   public void OnResponse(HttpRequestMessage request, HttpResponseMessage response)
   {
      response.StatusCode = HttpStatusCode.Accepted;
   }
}
client.AddInterceptor(new DummyInterceptor());

Bugs

If you encounter a bug, performance issue, or malfunction, please add an Issue with steps on how to reproduce the problem.

TODO

  • Add more tests
  • Add more documentation

License

Code and documentation are available according to the MIT License (see LICENSE).

@ziλasal

httwrap's People

Contributors

bugthesystem avatar ocinbat avatar ozzimpact avatar previousdeveloper avatar gokgokalp avatar volkanakinpasa avatar

Stargazers

vuduongtp avatar Abdelrahman Gobarah avatar 天梯800分 avatar Alex Doutsinis avatar Clive Harding avatar frankfanslc avatar Alameen avatar Tomislav Valent avatar pedoc avatar Jeffrey Loiola avatar Alberto G avatar dev avatar  avatar d0apga avatar Xu avatar Jack Chang avatar Ankit Sharma avatar Pedro Fernandes avatar  avatar Zekeriya Keskin avatar Ashish Vishwakarma avatar Alexander Ivanov avatar barbosatek avatar  avatar Arpit Nagar avatar tianfeng avatar Gabriel M avatar  avatar Navneet Singh avatar Thom Bakker avatar yang.tony avatar  avatar  avatar iwyc avatar  avatar  avatar Catcher Wong avatar  avatar Rafael Escobar avatar Rhys Williams avatar  avatar  avatar Ersen Tekin avatar  avatar  avatar

Watchers

 avatar Emerson avatar  avatar Mustafa Ülkü avatar  avatar  avatar Tomislav Valent avatar 一条大狗 avatar  avatar Manuel Angueira avatar

httwrap's Issues

Using Httwrap with list of same objects

Hi ziyasal!

I have a problem/question.. If in my controller i have Http Get method ..

    [HttpGet("/api/Visit/AccessPoints/")]
    public IEnumerable<Visit> GetByAccessPoints([FromQuery]List<Guid> id)
    {
        return visitService.GetByAccessPoints(id);
    }

Endpoint is something like..
https://localhost:44388/api/Visit/AccessPoints?id=98fa2138-e2af-4dea-ad63-dfec82df1131&id=39d8f189-ceea-4789-af21-3700989d906c&id=f4e9930c-6f67-40e6-90b4-b507581047d8

So, as you can see I have a list of ids/guids... How I am using httwrap?
If I use
await httpWrapper.GetAsync<IEnumerable<Visit>>(url, ids);

queryStringBuilder doesn't build right url

Thanx in advance

Microsoft.Threading.Tasks cannot be installed from Nuget Httwrap package

Hi Ziya,
I cannot install httwrap package correctly in my 4.5.1 Library Project
httwrap-pacakge-issue

after installation I could not see Microsoft.Threading.Tasks under references
I get the following error:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

P.S. I installed in a console application successfuly

Thanks

GetAsync can't use with await

HI,ziyasal:

When I use Httwrap to request server

IHttwrapConfiguration configuration = new HttwrapConfiguration(ServerApiAddress.API_HOST); IHttwrapClient _httwrap = new HttwrapClient(configuration);
var _response = await _httwrap.GetAsync(ServerApiAddress.VERSIFY_CODE);

here show follow error

Error CS4033: The await' operator can only be used when its containing method is marked with theasync' modifier (CS4033) (gp2_mobile)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.