How should one write a library that is responsible for connecting to a restful api? The lib should provide methods for direct access for the api functions. Is using WebClient
to get the response as string and Json.NET
to deserialize them as objects a reliable, acceptable way of doing this?
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;//the server uses a self signed SSL cert
using (WebClient client = new WebClient())
{
client.Headers.Add("..", "....");
string str = client.DownloadString("https://..../users/"+UserId+"/super-secret-data");
}
you can make a class for each group of calls (user management, up/downloading data,...). this class implements some functions (mapping the web calls). so when you want to list all new member, then you can write
var accounting = new Accounting();
var memberList = accounting.GetNewMember();
This function ( GetNewMember() ) is simply a wrapper. In the function you call the REST-full API get the JSON back an convert it into the result you want.
And for creating the URIs, you can use the string.Format(string, object[]) overloading.
您可以这样做,也可以将代码生成与Visual Studio和swagger结合使用,以在解决方案中导入其余的Web api。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.