iQuantile.DotLib.OAA
1.0.2
dotnet add package iQuantile.DotLib.OAA --version 1.0.2
NuGet\Install-Package iQuantile.DotLib.OAA -Version 1.0.2
<PackageReference Include="iQuantile.DotLib.OAA" Version="1.0.2" />
paket add iQuantile.DotLib.OAA --version 1.0.2
#r "nuget: iQuantile.DotLib.OAA, 1.0.2"
// Install iQuantile.DotLib.OAA as a Cake Addin #addin nuget:?package=iQuantile.DotLib.OAA&version=1.0.2 // Install iQuantile.DotLib.OAA as a Cake Tool #tool nuget:?package=iQuantile.DotLib.OAA&version=1.0.2
Object API Helper
Object API helper is designed for Object Oriented programmers to call RESTful API's using Class Objects.Let the Library handle the rest!
Please feel free to fork and submit pull requests to the develop branch.
Installation
It is recommended to use NuGet. F.ex through the VS Package Manager Console Install-Package iQuantile.DotLib.OAA -Version 1.0.2
or using the VS "Manage NuGet Packages..." extension.
How to use
Create a model class of the API you wish to call. For example we are going to use a open API Battuta that gives us Country list.
Class Model
public class Country
{
public string name { get; set; }
public string code { get; set; }
}
Constructor
Now we need to create an instance of the API Helper class.You need to pass the class object template that you wish to request over API's and pass the base url through the constructor.
using System.Windows.Forms;
using ObjectAPIAssistant;
namespace Test.App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ApiAssistant<Country> assistant=new ApiAssistant<Country>("http://battuta.medunes.net/api/");
}
}
}
But the API we wish to call returns a list of Countries. So in that case we just need to pass a list of countries as the template.
using System.Collections.Generic;
using System.Windows.Forms;
using ObjectAPIAssistant;
namespace Test.App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ApiAssistant<List<Country>> assistant=new ApiAssistant<List<Country>>("http://battuta.medunes.net/api/");
}
}
}
GET
Now our API helper is ready. We need to update the request URI. Get your API key from Battuta if you are going to test it with that api
assistant.RequestUri = "country/all/?key={YOUR_API_KEY}";
Now lets get that list of Countries from them. In one line of code.
Countries = await assistant.GetObjectAsync();
Incase you wish to get a HTTP Response Message object and handle the Message Manually
HttpResponseMessage message = await assistant.GetResponseAsync();
POST
Posting works the same way as get method. But make sure you Update your request URI before calling any other function
assistant.RequestUri = "REQUEST_URI_HERE";
Pass the list of objects and it will return the list of objects after created, Given that the API provider returns the list.
Countries = await assistant.CreateObjectAsync(Countries);
Incase you wish to get a HTTP Response Message object and handle the Message Manually
HttpResponseMessage message = await assistant.CreateObjectResponseAsync(Countries);
File Upload
Files can be uploaded as StreamContent. Update the request URI just like before. And pass the File path in the function.File name and key value. Default key="file"
OpenFileDialog dialog=new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
await assistant.UploadFileStream(dialog.FileName, "File.csv","file");
}
PUT
PUT works exactly the same way as POST.
Pass the list of objects and it will return the list of objects after updated, Given that the API provider returns the list.
assistant.RequestUri = "REQUEST_URI_HERE";
Countries = await assistant.UpdateObjectAsync(Countries);
Incase you wish to get a HTTP Response Message object and handle the Message Manually
HttpResponseMessage message = await assistant.UpdateObjectResponseAsync(Countries);
DELETE
DELETE works a bit differently. It Takes only the id as input and returns a Status Code.
assistant.RequestUri = "REQUEST_URI_HERE";
HttpStatusCode code=await assistant.DeleteObjecAsync("ID");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
.NETFramework 4.0
- log4net (>= 1.2.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added File upload function