Integration of calls to external API interfaces in the WinForm hybrid framework
In our regular business process, the general internal processing interface is mostly database-related, based on the hybrid development of the WinForm development framework, although in the client invocation, the general choice is also based on Web API calls, but the backend we may not only for our business database processing, can also be called other external interfaces, such as logistics, supplier interface, and so on, this essay is mainly about how to integrate the external API interface based on the hybrid development framework.
1. Introduction to the structure of the hybrid frame
We know that a hybrid framework is a combination of several ways in which a client can access a Web API service, a WCF service, or a direct connection to a database, and in particular the Web API as the broadest application, the structure of its entire framework is as follows.
In the client, through the unified factory class callerfactory<t> to the corresponding interface access, here is mainly the server-side Web API Service Interface processing, as well as the client to the Web API interface encapsulation, two parts through some base classes to simplify processing, Can greatly improve the development efficiency.
For external third-party Web API interface, we can also be wrapped in our own Web API interface, so that the client through the corresponding interface to interact, do not need to ignore the internal or external interface, so as to achieve transparent interface calls.
2. Integrated processing of RFID external interface
In a customer application case, it is necessary to integrate the service provider RFID interface to achieve the corresponding data interaction, this essay is also based on this case analysis and operation of the entire process, so that we understand how to integrate the third-party Web API interface in the hybrid framework for our internal framework.
In general, the Web API interface requires a clear API URL, data submission method (Post/get), commit parameters, return collection, and some special data, and the operation of the general interface, but also need an access token, these are the key to the Web API interface calls.
Basically, we have the information on the 1/2/3 steps of the Web API above that can be programmed by the interface, which is very important for Web API development.
We need to be particularly important to the information in step 1
The token in this is the additional interface information, is the need to set the HTTP request header information inside, is the user identity of important information, so we generally need to first through the specified authorization interface to obtain this token information.
In this external interface collection, we find the interface definition for unified login authentication as shown below.
From the above analysis, we first need to handle the login authentication interface, and then pass token token token to other interfaces for data processing through the interface.
In combination with our hybrid framework, here I take the test Project TestProject project as an example, we adjust the corresponding class of the WHC.TestProject.Caller project as shown below.
Where the facade layer interface class IRFIDService.cs code is shown below.
<summary>/// RFID service External interface/// </summary> [ServiceContract] public interface Irfidservice {//<summary>/// End- User Unified login Verification/// </summary> [OperationContract] checkinresult CheckIn (string Username, string password, string device_uuid, String device_type, String last_app_ Version, string app_id); <summary>/// get label shipping notifications in bulk/// </summary> [OperationContract] Tagorderasnresult tagorderasn (int brand_id, string factcode, String start_time, String end_time, Pagerinfo pagerinfo, str ing token); <summary>/// Label Order Outbound Logistics information Write-back/// </summary> [OperationContract] Commonresult Tagoutpost (String docno_asn, String Factcode, String dest_factcode, list<freightinfo> Freight, string token); }
In this interface definition, we are based on input parameters, output parameters are defined, and token is an additional token parameter, used to request the header to write information.
The definition of this interface is not much different from our regular Web API interface definition, as follows is an internal customer information interface definition.
<summary>/// service Interface for customer information/// </summary> [ServiceContract] public interface icustomerservice:ibaseservice<customerinfo> {//<summary>///Customer list by Customer name/// < /summary>// <param name= "name" > Customer name </param>// <returns></returns> [ OperationContract] list<customerinfo> findbyname (string name); }
The difference is that they have different interface inheritance classes, and the external interface does not need to inherit the Ibaseservice interface because it does not have to deal with the database
Based on the definition of these interfaces, we also need to implement our specific Web API service, which is logically encapsulated in an external Web API interface, but for the client it is not necessary to know whether the interface is internal or external, and the client only needs to know if the parameters or the results can be submitted.
Because the Web API involves data submissions for multiple parameters, this is generally done in post, and data parameters are uniformly passed through the Web API by defining a Jobject object, and the Web API interface definition for login authentication is as follows.
<summary>///RFID-based application interface///</summary> public class Rfidcontroller:baseapicontroller { <summary>////End User Unified login verification///</summary>//<param name= "param" > contains multiple attributes Objects </param>//<param name= "token" > Access token </param> [httppost] public Checkinresult Che Ckin (jobject param) {checkinresult result = null; Dynamic obj = param; if (obj! = null) {//uses the post data var postdata = param. ToJson (); Use the specific URL var queryurl = "Https://***.***.***/api/v6/rfid/terminal/checkin/post"; var helper = new Httphelper (); Helper. ContentType = "Application/json"; var content = Helper. Gethtml (Queryurl, PostData, true); rfidbasedata<checkinresult> Jsonresult = jsonconvert.deserializeobject<rfidbasedata<checkinresult> > (cOntent); if (Jsonresult! = null && Jsonresult.code = = 0) {result = Jsonresult.data; } return result; } else {throw new myapiexception ("Pass parameter Error"); } }
The parameters entered here with the jobject param parameters, we submit to the external Web API interface, we put this parameter again in JSON format string can be:
var postdata = param. ToJson ();
The Checkinresult and Rfidbasedata are the entity class definitions based on input parameters and output results, which are designed to be serialized into strongly typed entity classes to facilitate data processing operations.
On the client side, we only need to dock good and Web API server interface, then it is very convenient to call, where the corresponding Web API interface client wrapper class Rfidcaller as shown below.
<summary>///interface call to RFID control Package///</summary> public class Rfidcaller:normalapiservice, Irfid Service {public Rfidcaller () {this. Configurationpath = Apiconfig.configfilename; Web API configuration File This.configurationname = Apiconfig.rfid; Public Checkinresult CheckIn (string Username, string password, string device_uuid, String Device_type, String las T_app_version, String app_id) {var action = System.Reflection.MethodBase.GetCurrentMethod (). Name; String url = Getnormalurl (action); var postdata = new {username = username, password = password, dev Ice_uuid = device_uuid, Device_type = device_type, last_app_version = Last_app_version, app_id = app_id,}. ToJson (); var result = Jsonhelper<checkinresult>. Convertjson (URL, postdata); Return rEsult; }
With these, we are directly in the client interface, we can call callerfactory<t> for processing operations, the following is the client form to obtain the code to verify the identity token data
private string token = null;//access to the RFID interface token///<summary>/// for access token based on end user Unified login verification// </ summary> //<returns></returns> private String Getrfidtoken () { string username = " Wuhuacong "; string password = "123456"; String device_uuid = "AAAAAAA"; String device_type = "iphone"; String last_app_version = "xxxxxxx"; String app_id = "ntdf5543581a2f066e74cf2fe456"; var result = Callerfactory<irfidservice>. Instance.checkin (username, password, device_uuid, Device_type, Last_app_version, app_id); if (result! = null) { token = Result.token; } return token; }
The above is an interface for authenticating identities, other types of interfaces are similar to the processing methods, such as adding a
Get Label Issue notification batch
After the operation, the corresponding client encapsulation class is shown below.
<summary>///interface call to RFID control Package///</summary> public class Rfidcaller:normalapiservice, Irfid Service {public Rfidcaller () {this. Configurationpath = Apiconfig.configfilename; Web API configuration File This.configurationname = Apiconfig.rfid; Public Checkinresult CheckIn (string Username, string password, string device_uuid, String Device_type, String las T_app_version, String app_id) {var action = System.Reflection.MethodBase.GetCurrentMethod (). Name; String url = Getnormalurl (action); var postdata = new {username = username, password = password, dev Ice_uuid = device_uuid, Device_type = device_type, last_app_version = Last_app_version, app_id = app_id,}. ToJson (); var result = Jsonhelper<checkinresult>. Convertjson (URL, postdata); Return rEsult; } public tagorderasnresult tagorderasn (int brand_id, string factcode, String start_time, String end_time, Pager.ent ity. Pagerinfo Pagerinfo, string token) {var action = System.Reflection.MethodBase.GetCurrentMethod (). Name; String url = Getnormalurl (action) + string. Format ("? Token={0}", token); var postdata = new {page = Pagerinfo.currenetpageindex, pagesize = Pagerinfo.pag Esize, brand_id = brand_id, Factcode = factcode, start_time = Start_time, End_time = End_time,}. ToJson (); var result = Jsonhelper<tagorderasnresult>. Convertjson (URL, postdata); return result; }
Get Label Issue notification batch
The following code defines the Web API interface
<summary>///Get label shipping notice in bulk///</summary>//<param name= "param" ></p aram>//<param name= "token" ></param>///<returns></returns> [HttpPost] Public Tagorderasnresult tagorderasn (jobject param, string token) {Tagorderasnresult result = Nu ll Dynamic obj = param; if (obj! = null) {//using the Post method var postdata = param. ToJson (); var queryurl = "Https://***.***.***/api/v6/rfid/tag/tag_order_asn/get"; var helper = new Httphelper (); Helper. ContentType = "Application/json"; Helper. Header.add ("token", token); var content = Helper. Gethtml (Queryurl, PostData, true); rfidbasedata<tagorderasnresult> Jsonresult = jsonconvert.deserializeobject<rfidbasedata< Tagorderasnresult>> (cOntent); if (Jsonresult! = null && Jsonresult.code = = 0) {result = Jsonresult.data; } return result; } else {throw new myapiexception ("Pass parameter Error"); }
Where the table header information, we specify by the following code, set special token header information
var helper = new Httphelper (); Helper. ContentType = "Application/json";
In the client's call form, we can get the data of the interface by invoking the corresponding interface.
Private Tagorderasnresult Asnresult; <summary>///// for labeling Production order bulk information///</summary>// <returns></returns> Private Tagorderasnresult GetResult () { Pagerinfo pagerinfo = new Pagerinfo () {PageSize = $, Currenetpageindex = 1};//Initializes a paging condition of var brand_id = this.txtbrand_id. Text.toint32 (); var factcode = This.txtfactcode.Text; var start_time = This.txtstart_time. DateTime.ToString ("Yyyy-mm-dd HH:mm:ss"); var end_time = This.txtend_time. DateTime.ToString ("Yyyy-mm-dd HH:mm:ss"); Asnresult = Callerfactory<irfidservice>. INSTANCE.TAGORDERASN (brand_id, Factcode, Start_time, End_time, Pagerinfo, Token); return asnresult; }
Through the above code demonstration, we understand the method of adding an external Web API interface on the basis of the hybrid framework, by adding the facade layer interface, adding the Web API interface, and the corresponding client encapsulation class, the specific processing parameters according to the input parameters of the Web API interface, Information such as output data can be processed comprehensively.
Finally, let's take a look at the data display interface.
Integration of calls to external API interfaces in the WinForm hybrid framework