ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
adctk.http_publisher.HttpPublisher Class Reference

Web server publisher implementation using the requests library. More...

Inheritance diagram for adctk.http_publisher.HttpPublisher:
Inheritance graph
[legend]
Collaboration diagram for adctk.http_publisher.HttpPublisher:
Collaboration graph
[legend]

Public Member Functions

 __init__ (self)
 
 post_method (self)
 
 old_publish (self, builder)
 
int config (self, **kwargs)
 
int initialize (self)
 Set up any resources needed for reuse in publish().
 
int publish (self, Builder b)
 
 publish_str (self, json_string)
 
 publish_obj (self, obj)
 
None finalize (self)
 Clean up any resources set up in initialize (files, pipes, etc).
 
- Public Member Functions inherited from adctk.publisher.Publisher
str name (self)
 Get the name which a factory or error message should use.
 
str version (self)
 
dict[str, str] get_option_defaults (self)
 
None pause (self)
 Cause publish calls to be ignored until resume is called.
 
None resume (self)
 Cancel an prior calls to pause.
 

Public Attributes

 plugin_prefix
 
 mode
 
 state
 
- Public Attributes inherited from adctk.publisher.Publisher
 defaults
 
 plugin_prefix
 
 paused
 
 state
 
 mode
 

Protected Attributes

 _base_url
 
 _base_port
 
 _timeouts
 
 _headers
 
 _post_method
 

Additional Inherited Members

- Static Public Member Functions inherited from adctk.publisher.Publisher
tuple[float] get_timeout_defaults ()
 Get the adc server connect and response timeouts defined in the environment ADC_SERVER_CONNECT_TIMEOUT and ADC_SERVER_POST_TIMEOUT as a tuple or default values if not defined.
 
str get_url_default ()
 Get the alleged adc server url base from environment ADC_SERVER_URL or the default if it is not present.
 
str get_port_default ()
 Get the adc server port number to use if it is properly defined in the environment ADC_SERVER_PORT or the default if it is not.
 
- Static Public Attributes inherited from adctk.publisher.Publisher
 API_VERSION = adc_version.AdcVersion()._dict()
 
- Protected Member Functions inherited from adctk.publisher.Publisher
str _get (self, dict[str, str] opts, str field)
 
bool _get_bool (self, dict[str, str] opts, str field)
 
- Static Protected Attributes inherited from adctk.publisher.Publisher
str _name = "unknown"
 
str _version = "0.0.0"
 

Detailed Description

Web server publisher implementation using the requests library.

This plugin generates a message sends it to the configured web service. Multiple independent instances of this plugin may be used simultaneously. Timeouts are applied, so applications will not hang on failures.

Environment Variables

Constructor & Destructor Documentation

◆ __init__()

adctk.http_publisher.HttpPublisher.__init__ (   self)

Reimplemented from adctk.publisher.Publisher.

65 def __init__(self):
66 super().__init__()
67 self.plugin_prefix = "ADC_HTTP_PLUGIN_"
68 self._base_url = Publisher.get_url_default()
69 self._base_port = Publisher.get_port_default()
70 self._timeouts = Publisher.get_timeout_defaults()
71 self._headers = {"Content-type" : "application/json", "Accept" : "application/json"}
72 self._post_method = None
73

Member Function Documentation

◆ config()

int adctk.http_publisher.HttpPublisher.config (   self,
**  kwargs 
)
 The config options for the http plugin are url=<URL>,
    port=<PORT NUMBER>, timeouts=<timeout_connect:timeout_post>.
    If not specified, then the environment variables ADC_HTTP_PLUGIN_*
    are checked, where * is URL or PORT or TIMEOUTS.
    Bad timeout values will be ignored.

Reimplemented from adctk.publisher.Publisher.

89 def config(self, **kwargs) -> int:
90 """ The config options for the http plugin are url=<URL>,
91 port=<PORT NUMBER>, timeouts=<timeout_connect:timeout_post>.
92 If not specified, then the environment variables ADC_HTTP_PLUGIN_*
93 are checked, where * is URL or PORT or TIMEOUTS.
94 Bad timeout values will be ignored.
95 """
96 self._base_url = self._get(kwargs, "URL")
97 self._base_port = self._get(kwargs, "PORT")
98 times = self._get(kwargs, "TIMEOUTS")
99 if isinstance(times, str):
100 tlist = times.split(":")
101 if len(tlist) == 1:
102 try:
103 t = float(tlist[0])
104 self._timeouts = (t,t)
105 except ValueError as e:
106 logger.error("adctk.HttpPublisher.config: valid timeouts %s not "\
107 "found in %s%s", times, self.plugin_prefix, "TIMEOUTS")
108 try:
109 ct = float(tlist[0])
110 pt = float(tlist[1])
111 self._timeouts = (ct,pt)
112 except ValueError as e:
113 logger.error("adctk.HttpPublisher.config: valid timeouts %s not "\
114 "found in %s%s", times, self.plugin_prefix, "TIMEOUTS")
115 else:
116 self._timeouts = times
117 self.mode = Publisher.Mode.INIT
118 return 0
119

◆ finalize()

None adctk.http_publisher.HttpPublisher.finalize (   self)

Clean up any resources set up in initialize (files, pipes, etc).

Reimplemented from adctk.publisher.Publisher.

168 def finalize(self) -> None:
169 return None

◆ initialize()

int adctk.http_publisher.HttpPublisher.initialize (   self)

Set up any resources needed for reuse in publish().

Reimplemented from adctk.publisher.Publisher.

120 def initialize(self) -> int:
121 if self.mode == Publisher.Mode.CONFIG:
122 self.config()
123 if self.mode != Publisher.Mode.INIT:
124 self.state = Publisher.State.ERR
125 return errno.EINVAL
126 self.mode = Publisher.Mode.PUB_OR_FINAL
127 return 0
128

◆ old_publish()

adctk.http_publisher.HttpPublisher.old_publish (   self,
  builder 
)
publish json extracted from builder to _base_url server
81 def old_publish(self, builder):
82 """publish json extracted from builder to _base_url server"""
83 url = f"{self._base_url}:{self._base_port}/log"
84 ret = http_post(self.post_method, url, self._headers,
85 builder.get_json_dump(), self._timeouts)
86 logger.info(f"Returned json: {ret.json()}")
87 return ret
88

◆ post_method()

adctk.http_publisher.HttpPublisher.post_method (   self)
@return requests.post, or an alternative if debugging
75 def post_method(self):
76 """@return requests.post, or an alternative if debugging"""
77 if not self._post_method:
78 self._post_method=requests.post
79 return self._post_method
80

◆ publish()

int adctk.http_publisher.HttpPublisher.publish (   self,
Builder  b 
)
publish json extracted from builder.

Reimplemented from adctk.publisher.Publisher.

129 def publish(self, b: Builder) -> int:
130 """publish json extracted from builder."""
131 if self.paused:
132 return 0
133 if self.state != Publisher.State.OK:
134 return 1
135 if self.mode != Publisher.Mode.PUB_OR_FINAL:
136 return 2
137 url = f"{self._base_url}:{self._base_port}/log"
138 ret = http_post(self.post_method, url, self._headers, b.get_json_dump(), self._timeouts)
139 logger.info(f"Returned json: {ret.json()}")
140 return ret
141

◆ publish_obj()

adctk.http_publisher.HttpPublisher.publish_obj (   self,
  obj 
)
publish obj from caller to _base_url server

Reimplemented from adctk.publisher.Publisher.

155 def publish_obj(self, obj):
156 """publish obj from caller to _base_url server"""
157 if self.paused:
158 return 0
159 if self.state != Publisher.State.OK:
160 return 1
161 if self.mode != Publisher.Mode.PUB_OR_FINAL:
162 return 2
163 url = f"{self._base_url}:{self._base_port}/log"
164 ret = http_post(self.post_method, url, self._headers, json.dumps(obj), self._timeouts)
165 logger.info(f"Returned json: {ret.json()}")
166 return ret
167

◆ publish_str()

adctk.http_publisher.HttpPublisher.publish_str (   self,
  json_string 
)
publish json_raw from caller to _base_url server

Reimplemented from adctk.publisher.Publisher.

142 def publish_str(self, json_string):
143 """publish json_raw from caller to _base_url server"""
144 if self.paused:
145 return 0
146 if self.state != Publisher.State.OK:
147 return 1
148 if self.mode != Publisher.Mode.PUB_OR_FINAL:
149 return 2
150 url = f"{self._base_url}:{self._base_port}/log"
151 ret = http_post(self.post_method, url, self._headers, json_string, self._timeouts)
152 logger.info(f"Returned json: {ret.json()}")
153 return ret
154

Member Data Documentation

◆ _base_port

adctk.http_publisher.HttpPublisher._base_port
protected

◆ _base_url

adctk.http_publisher.HttpPublisher._base_url
protected

◆ _headers

adctk.http_publisher.HttpPublisher._headers
protected

◆ _post_method

adctk.http_publisher.HttpPublisher._post_method
protected

◆ _timeouts

adctk.http_publisher.HttpPublisher._timeouts
protected

◆ mode

adctk.http_publisher.HttpPublisher.mode

◆ plugin_prefix

adctk.http_publisher.HttpPublisher.plugin_prefix

◆ state

adctk.http_publisher.HttpPublisher.state

The documentation for this class was generated from the following file: