Tech Tip: How to Implement an HTTP Agent as a Singleton
PRODUCT: 4D | VERSION: 20 R6 | PLATFORM: Mac & Win
Published On: October 14, 2024
HTTP Agents are a new feature for 4D added in version 20 R6. They are a tool that can allow developers to customize and optimize HTTP connections. Developers may use the same HTTP agent for managing requests sent from several different parts of their code. In this case it may be convenient to implement the HTTP agent using a singleton so that different parts of the code can easily access the same HTTP agent. Below is an example of how a singleton class constructor could be implemented in a class named "http_agent":
singleton Class constructor() var $options:={} $options.maxSockets:=5 //5 is the maximum number of sockets per server $options.maxTotalSockets:=10 //10 is the maximum number of sockets for the agent $options.validateTLSCertificate:=True //To validate the sever's certificate This.myAgent:=4D.HTTPAgent.new($options) |
To send a request using this singleton HTTP agent the following code can be execute:
var $options:={} $options.method:="GET" $options.agent:= cs.http_agent.me.myAgent var $myRequest:=4D.HTTPRequest.new("www.example.com"; $options) |