WinForm application interface development practice – how to integrate external API interface calls in a hybrid framework

In our regular business processing, most of the internal processing interfaces are database-related and based on the Winform development framework of hybrid development. Although when the client calls, the general choice is also based on Web API calls, but the back-end We may not only process our business database, but also call other external interfaces, such as logistics, supplier interfaces, etc. This article mainly introduces how to integrate calls to external API interfaces based on a hybrid development framework.

PS: I would like to recommend an interface component that can be used in C# development – DevExpress WinForms. It can perfectly build smooth, beautiful and easy-to-use applications, whether it is an Office-style interface or analyzing and processing large quantities of business data. All can be done easily!

Click to get the official version of DevExpress v23.1(Q technical communication: 909157416)

1. Introduction to the structure of hybrid framework

The hybrid framework is a combination of several ways to access Web API services, WCF services, or directly connect to the database on the client side. Web API is the most widely used. The structure of the entire framework is as follows.

Integrate calls to external API interfaces in the Winform hybrid framework

Integrate calls to external API interfaces in the Winform hybrid framework

In the client, the corresponding interface is accessed through the unified factory class CallerFactory. This is mainly the processing of the server-side Web API service interface and the client’s encapsulation of the Web API interface. The two parts are performed through some base classes. Simplifying processing can greatly improve development efficiency.

For the external third-party Web API interface, we can also wrap it in our own Web API interface, so that the client can interact through the corresponding interface, without caring about the internal or external interface, thereby achieving transparent interface calls.

2. Integration processing of RFID external interface

In a customer’s application case, it is necessary to integrate the service provider’s RFID interface to achieve corresponding data interaction. This article is also based on this case to analyze and operate the entire process, so that we can understand how to integrate third-party Web API interfaces in the hybrid framework for our internal used by the framework.

Generally speaking, the Web API interface needs to specify the API URL, data submission method (POST/GET), submission parameters, return collection, and some special data, etc., and the operation of the general interface also requires an access token. These are the keys to Web API interface calls.

Integrate calls to external API interfaces in the Winform hybrid framework

Integrate calls to external API interfaces in the Winform hybrid framework

Integrate calls to external API interfaces in the Winform hybrid framework

Basically, we can start interface programming with the information on steps 1/2/3 of Web API above. These are very important information for Web API development.

We need to pay special attention to the information in step 1:

Integrate calls to external API interfaces in the Winform hybrid framework

The token here is additional interface information, which needs to be set in the header information of the Http Request request. It is important information about the user’s identity, so we generally need to obtain this token information through the specified authorization interface first.

In this external interface collection, we find the interface definition for unified login authentication as shown below.

Integrate calls to external API interfaces in the Winform hybrid framework

Integrate calls to external API interfaces in the Winform hybrid framework

Through the above analysis, we first need to handle the login verification interface, and then pass the token through the interface to other interfaces for data processing.

Combined with our hybrid framework structure, here I will introduce the test project TestProject project as an example. We adjust the corresponding class of the WHC.TestProject.Caller project as shown below.

Integrate calls to external API interfaces in the Winform hybrid framework

The Facade layer interface class IRFIDService.cs code is as follows.

/// <summary>
/// RFID service external interface
/// </summary>
[ServiceContract]
public interface IRFIDService
{

/// <summary>
/// Unified login verification for end users
/// </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 notice batch
/// </summary>
[OperationContract]
TagOrderAsnResult TagOrderAsn(int brand_id, string factcode, string start_time, string end_time, PagerInfo pagerInfo, string token);

/// <summary>
/// Write back label order outbound logistics information
/// </summary>
[OperationContract]
CommonResult TagOutPost(string docno_asn, string factcode, string dest_factcode, List<FreightInfo> freight, string token);
}

The interface definition here is defined based on input parameters and output parameters. In addition, token is an additional token parameter used to write information in the request header.

The definition of this interface is actually not much different from our regular Web API interface definition. The following is an internal customer information interface definition.

/// <summary>
/// Service interface for customer information
/// </summary>
[ServiceContract]
public interface ICustomerService : IBaseService<CustomerInfo>
{
/// <summary>
/// Get the customer list based on customer name
/// </summary>
/// <param name="name">Customer name</param>
/// <returns></returns>
[OperationContract]
List<CustomerInfo> FindByName(string name);
}

The difference is that their interface inheritance classes are different. Since the external interface does not need to deal with the database, we do not need to inherit the IBaseService interface.

According to the definition of these interfaces, we also need to implement our specific Web API service. Logically, it is an encapsulation of the external Web API interface, but for the client, there is no need to know whether it is an internal or external interface. The client only needs Just know how to submit parameters or results.

Since Web API involves the data submission of multiple parameters, generally speaking, this situation is handled in POST mode. The data parameters are unified and passed on the Web API side by defining a JObject object. The Web API interface definition for login authentication As follows.

/// <summary>
/// RFID-based application interface
/// </summary>
public class RFIDController : BaseApiController
{
/// <summary>
/// Unified login verification for end users
/// </summary>
/// <param name="param">Object containing multiple attributes</param>
/// <param name="token">Access token</param>
[HttpPost]
public CheckinResult CheckIn(JObject param)
{
CheckinResult result = null;
dynamic obj = param;
if (obj != null)
{
//Use POST data
var postData = param.ToJson();
//Use 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 & amp; & amp; jsonResult.code == 0)
{
result = jsonResult.data;
}
return result;
}
else
{
throw new MyApiException("Error passing parameters");
}
}

The input parameters here use the JObject param parameter. When we submit it to the external Web API interface, we serialize this parameter into a string in JSON format:

var postData = param.ToJson();

Among them, CheckinResult and RFIDBaseData are entity class definitions based on input parameters and output results. The purpose is to serialize them into strongly typed entity classes to facilitate data processing operations.

On the client side, we only need to connect the interface with the Web API server, so it is very convenient to call. The corresponding Web API interface client encapsulation class RFIDCaller is as follows.

/// <summary>
/// Encapsulate the interface call of RFID control
/// </summary>
public class RFIDCaller : NormalApiService, IRFIDService
{
publicRFIDCaller()
{
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 last_app_version, string app_id)
{
var action = System.Reflection.MethodBase.GetCurrentMethod().Name;
string url = GetNormalUrl(action);
var postData = new
{
username = username,
password = password,
device_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 can perform processing operations by calling CallerFactory directly in the client interface. The following is the code for the client form to obtain the authentication token data:

private string token = null;//token for accessing the RFID interface
/// <summary>
/// Obtain relevant access tokens based on unified login verification of end users
/// </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 the interface for authenticating identity. Other types of interfaces are handled in a similar way, such as adding an

Get label shipment notification batch

After the operation, the corresponding client encapsulation class is as follows.

/// <summary>
/// Encapsulate the interface call of RFID control
/// </summary>
public class RFIDCaller : NormalApiService, IRFIDService
{
publicRFIDCaller()
{
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 last_app_version, string app_id)
{
var action = System.Reflection.MethodBase.GetCurrentMethod().Name;
string url = GetNormalUrl(action);
var postData = new
{
username = username,
password = password,
device_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.Entity.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.PageSize,
brand_id = brand_id,
factcode = factcode,
start_time = start_time,
end_time = end_time,
}.ToJson();

var result = JsonHelper.ConvertJson(url, postData);
return result;
}

Get label shipment notification batch

The Web API interface is defined in the following code

/// <summary>
/// Get label shipping notice batch
/// </summary>
/// <param name="param"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
public TagOrderAsnResult TagOrderAsn(JObject param, string token)
{
TagOrderAsnResult result = null;
dynamic obj = param;
if (obj != null)
{
//Use 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 & amp; & amp; jsonResult.code == 0)
{
result = jsonResult.data;
}

return result;
}
else
{
throw new MyApiException("Error passing parameters");
}

Among the header information, we specify it through the following code and set special token header information.

var helper = new HttpHelper();
helper.ContentType = "application/json";
helper.Header.Add("token", token);

In the client’s calling form, we can obtain the data of the interface by calling the corresponding interface.

private TagOrderAsnResult asnResult;
/// <summary>
/// Get label production order batch information based on parameters
/// </summary>
/// <returns></returns>
private TagOrderAsnResult GetResult()
{
PagerInfo pagerInfo = new PagerInfo() { PageSize = 50, CurrenetPageIndex = 1 };//Initialize a paging condition
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 have learned how to add an external Web API interface based on the hybrid framework. By adding a Facade layer interface, a Web API interface, and a corresponding client encapsulation class, the specific processing parameters are based on the input of the Web API interface. Parameters, output data and other information can be comprehensively processed.

Finally, let’s take a look at the data display interface.

Integrate calls to external API interfaces in the Winform hybrid framework

This article is reproduced from: Blog Garden – Wu Huacong