codecraftai-agent-kit 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (230) hide show
  1. ccai/agentkit/__init__.py +304 -0
  2. ccai/agentkit/addressresolver.py +177 -0
  3. ccai/agentkit/agent.py +2945 -0
  4. ccai/agentkit/agentaddress.py +187 -0
  5. ccai/agentkit/agentconfiguration.py +581 -0
  6. ccai/agentkit/agentidentity.py +152 -0
  7. ccai/agentkit/agentregistry.py +221 -0
  8. ccai/agentkit/agentstatemachine.py +134 -0
  9. ccai/agentkit/alternateproviderstrategy.py +36 -0
  10. ccai/agentkit/approvalgate.py +238 -0
  11. ccai/agentkit/approvalrequest.py +250 -0
  12. ccai/agentkit/asyncagent.py +187 -0
  13. ccai/agentkit/breakpoint.py +150 -0
  14. ccai/agentkit/builtinagentmetrics.py +114 -0
  15. ccai/agentkit/builtins/__init__.py +11 -0
  16. ccai/agentkit/builtins/filereadtool.py +90 -0
  17. ccai/agentkit/builtins/filewritetool.py +93 -0
  18. ccai/agentkit/builtins/httprequesttool.py +169 -0
  19. ccai/agentkit/builtins/shellcommandtool.py +190 -0
  20. ccai/agentkit/cachedresultstrategy.py +43 -0
  21. ccai/agentkit/checkpointmanager.py +219 -0
  22. ccai/agentkit/circuitbreaker.py +230 -0
  23. ccai/agentkit/circuitbreakerconfiguration.py +158 -0
  24. ccai/agentkit/circuitbreakerevent.py +158 -0
  25. ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
  26. ccai/agentkit/circuitbreakerregistry.py +116 -0
  27. ccai/agentkit/circuitbreakerstate.py +44 -0
  28. ccai/agentkit/clearcacheaction.py +46 -0
  29. ccai/agentkit/concurrencybridge.py +143 -0
  30. ccai/agentkit/concurrencymodel.py +19 -0
  31. ccai/agentkit/conditionevaluator.py +594 -0
  32. ccai/agentkit/configurationchangeevent.py +179 -0
  33. ccai/agentkit/configurationerrors.py +152 -0
  34. ccai/agentkit/configurationlayer.py +160 -0
  35. ccai/agentkit/configurationlayersource.py +46 -0
  36. ccai/agentkit/configurationmanager.py +680 -0
  37. ccai/agentkit/configurationschema.py +224 -0
  38. ccai/agentkit/configurationschemafield.py +217 -0
  39. ccai/agentkit/configurationserializer.py +163 -0
  40. ccai/agentkit/configurationvalidationresult.py +118 -0
  41. ccai/agentkit/contextsummarizer.py +170 -0
  42. ccai/agentkit/contextwindow.py +264 -0
  43. ccai/agentkit/contextwindowstrategy.py +57 -0
  44. ccai/agentkit/correlationcontext.py +156 -0
  45. ccai/agentkit/costestimate.py +139 -0
  46. ccai/agentkit/costtracker.py +121 -0
  47. ccai/agentkit/deadletterentry.py +134 -0
  48. ccai/agentkit/deadletterquery.py +119 -0
  49. ccai/agentkit/deadletterqueue.py +123 -0
  50. ccai/agentkit/deadletterstore.py +86 -0
  51. ccai/agentkit/debuginspector.py +211 -0
  52. ccai/agentkit/debugsession.py +223 -0
  53. ccai/agentkit/defaultvaluestrategy.py +43 -0
  54. ccai/agentkit/degradationchangeevent.py +120 -0
  55. ccai/agentkit/degradationlevel.py +44 -0
  56. ccai/agentkit/degradationmanager.py +212 -0
  57. ccai/agentkit/degradationpolicy.py +130 -0
  58. ccai/agentkit/degradationstrategy.py +66 -0
  59. ccai/agentkit/embeddingprovider.py +106 -0
  60. ccai/agentkit/errors.py +95 -0
  61. ccai/agentkit/executionrecord.py +213 -0
  62. ccai/agentkit/executionrecorder.py +180 -0
  63. ccai/agentkit/executionrecording.py +230 -0
  64. ccai/agentkit/executionreplayer.py +187 -0
  65. ccai/agentkit/expirationpolicy.py +116 -0
  66. ccai/agentkit/fallbackchain.py +82 -0
  67. ccai/agentkit/featureflag.py +154 -0
  68. ccai/agentkit/featureflagevaluator.py +109 -0
  69. ccai/agentkit/featureflagregistry.py +258 -0
  70. ccai/agentkit/filedeadletterstore.py +112 -0
  71. ccai/agentkit/filememorystore.py +291 -0
  72. ccai/agentkit/healthcheck.py +46 -0
  73. ccai/agentkit/healthcheckregistry.py +51 -0
  74. ccai/agentkit/healthmonitor.py +258 -0
  75. ccai/agentkit/healthstatus.py +105 -0
  76. ccai/agentkit/hotreloadmonitor.py +214 -0
  77. ccai/agentkit/hotreloadpolicy.py +160 -0
  78. ccai/agentkit/inmemorydeadletterstore.py +61 -0
  79. ccai/agentkit/inmemorystore.py +173 -0
  80. ccai/agentkit/jsonmessageserializer.py +89 -0
  81. ccai/agentkit/jsonoutputparser.py +50 -0
  82. ccai/agentkit/lifecyclestate.py +22 -0
  83. ccai/agentkit/llmconversation.py +153 -0
  84. ccai/agentkit/llmerrors.py +193 -0
  85. ccai/agentkit/llmmessage.py +158 -0
  86. ccai/agentkit/llmprovider.py +158 -0
  87. ccai/agentkit/llmproviderregistry.py +200 -0
  88. ccai/agentkit/llmresponse.py +190 -0
  89. ccai/agentkit/llmstreamchunk.py +65 -0
  90. ccai/agentkit/llmstreamhandler.py +48 -0
  91. ccai/agentkit/llmtoolbridge.py +154 -0
  92. ccai/agentkit/logexporter.py +316 -0
  93. ccai/agentkit/logformatter.py +169 -0
  94. ccai/agentkit/memoryconfiguration.py +243 -0
  95. ccai/agentkit/memoryentry.py +254 -0
  96. ccai/agentkit/memoryerrors.py +135 -0
  97. ccai/agentkit/memoryindex.py +213 -0
  98. ccai/agentkit/memoryquery.py +248 -0
  99. ccai/agentkit/memoryqueryresult.py +86 -0
  100. ccai/agentkit/memoryscope.py +54 -0
  101. ccai/agentkit/memorystore.py +260 -0
  102. ccai/agentkit/memorystorehealthcheck.py +33 -0
  103. ccai/agentkit/memorystoreregistry.py +106 -0
  104. ccai/agentkit/message.py +310 -0
  105. ccai/agentkit/messageenvelope.py +144 -0
  106. ccai/agentkit/messagepattern.py +50 -0
  107. ccai/agentkit/messagerouter.py +290 -0
  108. ccai/agentkit/messageserializer.py +106 -0
  109. ccai/agentkit/messageserializerregistry.py +163 -0
  110. ccai/agentkit/messagingconfiguration.py +397 -0
  111. ccai/agentkit/messagingerrors.py +338 -0
  112. ccai/agentkit/metricdefinition.py +164 -0
  113. ccai/agentkit/metricmeasurement.py +178 -0
  114. ccai/agentkit/metricscollector.py +258 -0
  115. ccai/agentkit/metricsexporter.py +303 -0
  116. ccai/agentkit/metricsregistry.py +130 -0
  117. ccai/agentkit/metricssnapshot.py +155 -0
  118. ccai/agentkit/metrictype.py +51 -0
  119. ccai/agentkit/msgpackmessageserializer.py +97 -0
  120. ccai/agentkit/observabilityconfiguration.py +546 -0
  121. ccai/agentkit/observabilityerror.py +67 -0
  122. ccai/agentkit/observabilitypipeline.py +252 -0
  123. ccai/agentkit/outputparser.py +82 -0
  124. ccai/agentkit/outputschema.py +142 -0
  125. ccai/agentkit/parallelbranch.py +87 -0
  126. ccai/agentkit/platform/__init__.py +43 -0
  127. ccai/agentkit/platform/adapters/__init__.py +18 -0
  128. ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
  129. ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
  130. ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
  131. ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
  132. ccai/agentkit/platform/adapters/platformadapter.py +139 -0
  133. ccai/agentkit/platform/deployment/__init__.py +15 -0
  134. ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
  135. ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
  136. ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
  137. ccai/agentkit/platform/errors.py +224 -0
  138. ccai/agentkit/platform/platformconfiguration.py +412 -0
  139. ccai/agentkit/platform/platformdetector.py +125 -0
  140. ccai/agentkit/platform/platformruntime.py +331 -0
  141. ccai/agentkit/platform/platformserviceregistry.py +149 -0
  142. ccai/agentkit/platform/platformtype.py +60 -0
  143. ccai/agentkit/platform/services/__init__.py +19 -0
  144. ccai/agentkit/platform/services/computeservice.py +112 -0
  145. ccai/agentkit/platform/services/invocationresult.py +173 -0
  146. ccai/agentkit/platform/services/messagingservice.py +135 -0
  147. ccai/agentkit/platform/services/platformservice.py +111 -0
  148. ccai/agentkit/platform/services/queuemessage.py +193 -0
  149. ccai/agentkit/platform/services/storageobject.py +195 -0
  150. ccai/agentkit/platform/services/storageservice.py +159 -0
  151. ccai/agentkit/pluginconfiguration.py +241 -0
  152. ccai/agentkit/plugindependency.py +129 -0
  153. ccai/agentkit/plugindescriptor.py +225 -0
  154. ccai/agentkit/pluginlifecyclehook.py +41 -0
  155. ccai/agentkit/pluginmanager.py +360 -0
  156. ccai/agentkit/pluginstatus.py +47 -0
  157. ccai/agentkit/pooledagent.py +116 -0
  158. ccai/agentkit/prompttemplate.py +221 -0
  159. ccai/agentkit/prompttemplateregistry.py +145 -0
  160. ccai/agentkit/protobufmessageserializer.py +144 -0
  161. ccai/agentkit/pubsubbroker.py +311 -0
  162. ccai/agentkit/py.typed +0 -0
  163. ccai/agentkit/reconnecttransportaction.py +47 -0
  164. ccai/agentkit/regexoutputparser.py +68 -0
  165. ccai/agentkit/requestresponsehandler.py +274 -0
  166. ccai/agentkit/resetcircuitbreakeraction.py +48 -0
  167. ccai/agentkit/resilienceconfiguration.py +191 -0
  168. ccai/agentkit/resilienceerrors.py +103 -0
  169. ccai/agentkit/resiliencepipeline.py +201 -0
  170. ccai/agentkit/retryattempt.py +158 -0
  171. ccai/agentkit/retryexecutor.py +157 -0
  172. ccai/agentkit/retrypolicy.py +195 -0
  173. ccai/agentkit/retryresult.py +186 -0
  174. ccai/agentkit/selfhealingaction.py +42 -0
  175. ccai/agentkit/selfhealingpolicy.py +67 -0
  176. ccai/agentkit/selfhealingresult.py +130 -0
  177. ccai/agentkit/stepexecutionstatus.py +52 -0
  178. ccai/agentkit/stepresult.py +198 -0
  179. ccai/agentkit/steps/__init__.py +9 -0
  180. ccai/agentkit/steps/agentstep.py +247 -0
  181. ccai/agentkit/steps/approvalstep.py +276 -0
  182. ccai/agentkit/steps/llmstep.py +403 -0
  183. ccai/agentkit/steps/subworkflowstep.py +270 -0
  184. ccai/agentkit/steps/toolstep.py +265 -0
  185. ccai/agentkit/structuredlogentry.py +222 -0
  186. ccai/agentkit/structuredlogger.py +189 -0
  187. ccai/agentkit/subscription.py +141 -0
  188. ccai/agentkit/threadedagent.py +94 -0
  189. ccai/agentkit/tokenusage.py +136 -0
  190. ccai/agentkit/tool.py +203 -0
  191. ccai/agentkit/toolchain.py +268 -0
  192. ccai/agentkit/toolchainexecutor.py +134 -0
  193. ccai/agentkit/toolchainresult.py +166 -0
  194. ccai/agentkit/tooldescriptor.py +203 -0
  195. ccai/agentkit/toolerrors.py +160 -0
  196. ccai/agentkit/toolexecutor.py +168 -0
  197. ccai/agentkit/toolfactory.py +60 -0
  198. ccai/agentkit/toolregistry.py +184 -0
  199. ccai/agentkit/toolresult.py +142 -0
  200. ccai/agentkit/toolschema.py +197 -0
  201. ccai/agentkit/tracecontext.py +294 -0
  202. ccai/agentkit/traceevent.py +166 -0
  203. ccai/agentkit/traceexporter.py +305 -0
  204. ccai/agentkit/tracer.py +234 -0
  205. ccai/agentkit/tracespan.py +349 -0
  206. ccai/agentkit/transitionrecord.py +84 -0
  207. ccai/agentkit/transport.py +334 -0
  208. ccai/agentkit/transportconfiguration.py +436 -0
  209. ccai/agentkit/transporthealthcheck.py +36 -0
  210. ccai/agentkit/transportregistry.py +224 -0
  211. ccai/agentkit/transports/__init__.py +28 -0
  212. ccai/agentkit/transports/grpctransport.py +410 -0
  213. ccai/agentkit/transports/httptransport.py +423 -0
  214. ccai/agentkit/transports/messagequeuetransport.py +489 -0
  215. ccai/agentkit/transports/websockettransport.py +473 -0
  216. ccai/agentkit/workflowcheckpoint.py +208 -0
  217. ccai/agentkit/workflowconfiguration.py +295 -0
  218. ccai/agentkit/workflowcontext.py +249 -0
  219. ccai/agentkit/workflowdefinition.py +790 -0
  220. ccai/agentkit/workflowedge.py +128 -0
  221. ccai/agentkit/workflowerrors.py +295 -0
  222. ccai/agentkit/workflowexecutionstatus.py +48 -0
  223. ccai/agentkit/workflowexecutor.py +1121 -0
  224. ccai/agentkit/workflowregistry.py +185 -0
  225. ccai/agentkit/workflowresult.py +250 -0
  226. ccai/agentkit/workflowstep.py +195 -0
  227. codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
  228. codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
  229. codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
  230. codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
@@ -0,0 +1,423 @@
1
+ """
2
+ Defines the HttpTransport class, a concrete Transport implementation
3
+ that uses HTTP POST semantics for sending messages between agents.
4
+
5
+ The HttpTransport uses Python's standard library urllib.request module
6
+ for HTTP operations. It constructs URLs from the TransportConfiguration
7
+ (host, port, path), serializes messages with the MessageSerializer for
8
+ the message's serialization format (JSON by default), and sends them via
9
+ HTTP POST. Configurable headers, TLS (via https:// URLs), and
10
+ credential resolution for Authorization headers are supported.
11
+
12
+ The transport raises TransportError for connection errors, timeouts,
13
+ and HTTP 5xx server error responses.
14
+
15
+ Structured logging is performed under the
16
+ ccai.agentkit.messaging.transport.http logger hierarchy.
17
+
18
+ .. module:: ccai.agentkit.transports.httptransport
19
+ :platform: Darwin, Linux, Unix, Windows
20
+ :synopsis: HTTP transport implementation using urllib for agent messaging.
21
+
22
+ Requirements: 16.1-16.8, 13.1-13.4, 25.2, 25.3, 25.4, 25.5, 25.6
23
+ """
24
+
25
+ import logging
26
+ import ssl
27
+ import urllib.error
28
+ import urllib.request
29
+
30
+ from typing import Dict
31
+ from typing import List
32
+ from typing import Optional
33
+
34
+ from ccai.agentkit.message import Message
35
+ from ccai.agentkit.messageenvelope import MessageEnvelope
36
+ from ccai.agentkit.messagingerrors import TransportError
37
+ from ccai.agentkit.transport import Transport
38
+ from ccai.agentkit.transportconfiguration import TransportConfiguration
39
+
40
+
41
+ class HttpTransport(Transport):
42
+ """
43
+ Transport implementation using HTTP POST semantics for sending
44
+ messages between agents.
45
+
46
+ Uses Python's standard library urllib.request for all HTTP
47
+ operations. The transport constructs the target URL from the
48
+ TransportConfiguration host, port, and optional path fields.
49
+ TLS is supported via https:// URL scheme when the configuration
50
+ options include ``{"tls": True}`` or the host starts with
51
+ "https://".
52
+
53
+ Configurable default headers are taken from the
54
+ TransportConfiguration headers dictionary. Credential resolution
55
+ for the Authorization header is supported through the
56
+ credential_references in the TransportConfiguration.
57
+
58
+ The connect method establishes a logical HTTP session by
59
+ validating the configuration and preparing the base URL and
60
+ default headers. The disconnect method tears down the session.
61
+
62
+ The send method serializes the message with the MessageSerializer
63
+ for the message's serialization format (JSON by default) and sends
64
+ it via HTTP POST to the recipient endpoint, with the serializer's
65
+ content type as the Content-Type header. For FIRE_AND_FORGET
66
+ pattern messages, the transport sends and returns immediately
67
+ with a "delivered" status envelope.
68
+
69
+ The receive method raises TransportError because HTTP is a
70
+ request-driven protocol and does not support passive message
71
+ reception.
72
+
73
+ TransportError is raised for connection errors, timeouts, and
74
+ HTTP 5xx server error responses.
75
+
76
+ :param name: The transport name string.
77
+ :param configuration: The TransportConfiguration instance
78
+ defining HTTP connection parameters.
79
+ :param credential_names: An optional list of credential
80
+ reference names required for authentication.
81
+ :param tags: An optional dictionary of tags for categorization
82
+ and discovery.
83
+ """
84
+
85
+ def __init__(self, name: str, configuration: TransportConfiguration,
86
+ credential_names: Optional[List[str]] = None,
87
+ tags: Optional[Dict[str, str]] = None) -> None:
88
+
89
+ super().__init__(
90
+ name=name,
91
+ protocol_type="http",
92
+ configuration=configuration,
93
+ credential_names=credential_names,
94
+ tags=tags
95
+ )
96
+
97
+ self._http_logger = logging.getLogger(
98
+ "ccai.agentkit.messaging.transport.http"
99
+ )
100
+ self._base_url = None
101
+ self._default_headers = {}
102
+ self._ssl_context = None
103
+
104
+ return
105
+
106
+ def _build_base_url(self) -> str:
107
+ """
108
+ Constructs the base URL from the TransportConfiguration
109
+ host, port, and optional path fields.
110
+
111
+ The URL scheme is determined by the TLS setting in the
112
+ configuration options. When ``options["tls"]`` is True,
113
+ the scheme is "https"; otherwise it is "http".
114
+
115
+ :returns: The base URL string.
116
+ """
117
+ config = self._configuration
118
+ options = config.options
119
+
120
+ use_tls = options.get("tls", False)
121
+ scheme = "https" if use_tls else "http"
122
+
123
+ host = config.host
124
+ port = config.port
125
+ path = config.path
126
+
127
+ base_url = "{}://{}:{}".format(scheme, host, port)
128
+
129
+ if path is not None and len(path.strip()) > 0:
130
+ clean_path = path.strip()
131
+ if not clean_path.startswith("/"):
132
+ clean_path = "/" + clean_path
133
+ base_url = base_url + clean_path
134
+
135
+ return base_url
136
+
137
+ def _build_ssl_context(self) -> Optional[ssl.SSLContext]:
138
+ """
139
+ Builds an SSL context when TLS is enabled in the
140
+ configuration options.
141
+
142
+ :returns: An ssl.SSLContext instance or None if TLS is
143
+ not enabled.
144
+ """
145
+ options = self._configuration.options
146
+ use_tls = options.get("tls", False)
147
+
148
+ if not use_tls:
149
+ return None
150
+
151
+ ssl_context = ssl.create_default_context()
152
+
153
+ return ssl_context
154
+
155
+ def _resolve_auth_header(self) -> Optional[str]:
156
+ """
157
+ Resolves the Authorization header value from the
158
+ credential references in the TransportConfiguration.
159
+
160
+ Looks for a credential reference named "authorization"
161
+ in the configuration's credential_references dictionary.
162
+ The credential reference value is used directly as the
163
+ Authorization header value.
164
+
165
+ :returns: The Authorization header value string or None
166
+ if no authorization credential is configured.
167
+ """
168
+ cred_refs = self._configuration.credential_references
169
+
170
+ if "authorization" in cred_refs:
171
+ auth_value = cred_refs["authorization"]
172
+ return auth_value
173
+
174
+ return None
175
+
176
+ def connect(self) -> None:
177
+ """
178
+ Establishes a logical HTTP session by validating the
179
+ configuration and preparing the base URL, default headers,
180
+ and optional SSL context.
181
+
182
+ Sets the transport to connected status upon success.
183
+
184
+ :raises TransportError: If the connection setup fails.
185
+ """
186
+ try:
187
+ self._base_url = self._build_base_url()
188
+ self._ssl_context = self._build_ssl_context()
189
+
190
+ self._default_headers = dict(self._configuration.headers)
191
+ self._default_headers["Content-Type"] = "application/json"
192
+
193
+ auth_header = self._resolve_auth_header()
194
+ if auth_header is not None:
195
+ self._default_headers["Authorization"] = auth_header
196
+
197
+ self._connected = True
198
+
199
+ self._http_logger.info(
200
+ "HTTP transport connected: name=%s, endpoint=%s",
201
+ self._name, self._base_url
202
+ )
203
+
204
+ except Exception as ex:
205
+ self._http_logger.warning(
206
+ "HTTP transport connect failed: name=%s, error=%s",
207
+ self._name, ex
208
+ )
209
+ raise TransportError(
210
+ transport_name=self._name,
211
+ operation="connect",
212
+ cause=ex
213
+ )
214
+
215
+ return
216
+
217
+ def disconnect(self) -> None:
218
+ """
219
+ Closes the logical HTTP session by clearing the base URL,
220
+ default headers, and SSL context.
221
+
222
+ Sets the transport to disconnected status.
223
+
224
+ :raises TransportError: If the disconnection fails.
225
+ """
226
+ try:
227
+ self._base_url = None
228
+ self._default_headers = {}
229
+ self._ssl_context = None
230
+ self._connected = False
231
+
232
+ self._http_logger.info(
233
+ "HTTP transport disconnected: name=%s",
234
+ self._name
235
+ )
236
+
237
+ except Exception as ex:
238
+ self._http_logger.warning(
239
+ "HTTP transport disconnect failed: name=%s, error=%s",
240
+ self._name, ex
241
+ )
242
+ raise TransportError(
243
+ transport_name=self._name,
244
+ operation="disconnect",
245
+ cause=ex
246
+ )
247
+
248
+ return
249
+
250
+ def send(self, message: Message) -> MessageEnvelope:
251
+ """
252
+ Sends a Message via HTTP POST to the recipient endpoint.
253
+
254
+ The message is serialized with the MessageSerializer that
255
+ resolve_serializer returns for the message's
256
+ serialization_format (JSON by default) and sent as the
257
+ request body with the serializer's content type as the
258
+ Content-Type header. The target URL is the base URL of this
259
+ transport. Default headers and any configured Authorization
260
+ header are included in the request.
261
+
262
+ For FIRE_AND_FORGET pattern messages, the transport sends
263
+ the message and returns immediately with a "delivered"
264
+ status envelope upon successful send.
265
+
266
+ :param message: The Message to send.
267
+
268
+ :returns: A MessageEnvelope with delivery metadata.
269
+
270
+ :raises TransportError: If the transport is not connected,
271
+ if the message cannot be serialized in its serialization
272
+ format, if a connection error occurs, if the request times
273
+ out, or if the server returns an HTTP 5xx response.
274
+ """
275
+ if not self._connected:
276
+ errmsg = "Transport '{}' is not connected.".format(self._name)
277
+ self._http_logger.warning(
278
+ "HTTP transport send failed: name=%s, reason=not connected",
279
+ self._name
280
+ )
281
+ raise TransportError(
282
+ transport_name=self._name,
283
+ operation="send",
284
+ cause=ConnectionError(errmsg)
285
+ )
286
+
287
+ try:
288
+ serializer = self.resolve_serializer(message.serialization_format)
289
+
290
+ message_data = message.as_dict()
291
+ body_bytes = serializer.serialize(message_data)
292
+
293
+ request = urllib.request.Request(
294
+ url=self._base_url,
295
+ data=body_bytes,
296
+ method="POST"
297
+ )
298
+
299
+ for header_name, header_value in self._default_headers.items():
300
+ request.add_header(header_name, header_value)
301
+
302
+ # The body is encoded by the serializer, so its content type
303
+ # replaces the default JSON Content-Type header.
304
+ request.add_header("Content-Type", serializer.content_type)
305
+
306
+ timeout_seconds = self._configuration.timeout
307
+
308
+ if self._ssl_context is not None:
309
+ urllib.request.urlopen(
310
+ request,
311
+ timeout=timeout_seconds,
312
+ context=self._ssl_context
313
+ )
314
+ else:
315
+ urllib.request.urlopen(
316
+ request,
317
+ timeout=timeout_seconds
318
+ )
319
+
320
+ self._http_logger.info(
321
+ "HTTP transport message sent: name=%s, message_id=%s, "
322
+ "sender=%s, recipient=%s, pattern=%s, format=%s",
323
+ self._name, message.identifier,
324
+ message.sender.agent_identifier,
325
+ message.recipient.agent_identifier,
326
+ message.pattern.value, serializer.format_name
327
+ )
328
+
329
+ envelope = MessageEnvelope(
330
+ message=message,
331
+ transport_name=self._name,
332
+ attempt_count=1,
333
+ delivery_status="delivered"
334
+ )
335
+
336
+ return envelope
337
+
338
+ except urllib.error.HTTPError as http_err:
339
+ if http_err.code >= 500:
340
+ self._http_logger.warning(
341
+ "HTTP transport send failed (5xx): name=%s, "
342
+ "message_id=%s, status_code=%s",
343
+ self._name, message.identifier, http_err.code
344
+ )
345
+ raise TransportError(
346
+ transport_name=self._name,
347
+ operation="send",
348
+ cause=http_err
349
+ )
350
+
351
+ self._http_logger.warning(
352
+ "HTTP transport send failed: name=%s, "
353
+ "message_id=%s, status_code=%s",
354
+ self._name, message.identifier, http_err.code
355
+ )
356
+ raise TransportError(
357
+ transport_name=self._name,
358
+ operation="send",
359
+ cause=http_err
360
+ )
361
+
362
+ except urllib.error.URLError as url_err:
363
+ self._http_logger.warning(
364
+ "HTTP transport send failed (connection error): name=%s, "
365
+ "message_id=%s, error=%s",
366
+ self._name, message.identifier, url_err
367
+ )
368
+ raise TransportError(
369
+ transport_name=self._name,
370
+ operation="send",
371
+ cause=url_err
372
+ )
373
+
374
+ except TimeoutError as timeout_err:
375
+ self._http_logger.warning(
376
+ "HTTP transport send failed (timeout): name=%s, "
377
+ "message_id=%s, error=%s",
378
+ self._name, message.identifier, timeout_err
379
+ )
380
+ raise TransportError(
381
+ transport_name=self._name,
382
+ operation="send",
383
+ cause=timeout_err
384
+ )
385
+
386
+ except Exception as ex:
387
+ self._http_logger.warning(
388
+ "HTTP transport send failed: name=%s, "
389
+ "message_id=%s, error=%s",
390
+ self._name, message.identifier, ex
391
+ )
392
+ raise TransportError(
393
+ transport_name=self._name,
394
+ operation="send",
395
+ cause=ex
396
+ )
397
+
398
+ def receive(self) -> Message:
399
+ """
400
+ Receives a Message from the transport.
401
+
402
+ HTTP is a request-driven protocol and does not support
403
+ passive message reception. This method always raises a
404
+ TransportError indicating that receive is not supported
405
+ for HTTP transports.
406
+
407
+ :returns: Never returns; always raises TransportError.
408
+
409
+ :raises TransportError: Always raised because HTTP does
410
+ not support passive receive.
411
+ """
412
+ self._http_logger.warning(
413
+ "HTTP transport receive not supported: name=%s",
414
+ self._name
415
+ )
416
+ raise TransportError(
417
+ transport_name=self._name,
418
+ operation="receive",
419
+ cause=NotImplementedError(
420
+ "HTTP transport does not support passive receive. "
421
+ "HTTP is a request-driven protocol."
422
+ )
423
+ )