MCPcopy Create free account
hub / github.com/aws/aws-cli / UserAgentComponent

Class UserAgentComponent

awscli/botocore/useragent.py:178–236  ·  view source on GitHub ↗

Component of a Botocore User-Agent header string in the standard format. Each component consists of a prefix, a name, a value, and a size_config. In the string representation these are combined in the format ``prefix/name#value``. ``size_config`` configures the max size and tr

Source from the content-addressed store, hash-verified

176
177
178class UserAgentComponent(NamedTuple):
179 """
180 Component of a Botocore User-Agent header string in the standard format.
181
182 Each component consists of a prefix, a name, a value, and a size_config.
183 In the string representation these are combined in the format
184 ``prefix/name#value``.
185
186 ``size_config`` configures the max size and truncation strategy for the
187 built user agent string component.
188 """
189
190 prefix: str
191 name: str
192 value: Optional[str] = None
193 size_config: Optional[UserAgentComponentSizeConfig] = None
194
195 def to_string(self):
196 """Create string like 'prefix/name#value' from a UserAgentComponent."""
197 clean_prefix = sanitize_user_agent_string_component(
198 self.prefix, allow_hash=True
199 )
200 clean_name = sanitize_user_agent_string_component(
201 self.name, allow_hash=False
202 )
203 if self.value is None or self.value == '':
204 clean_string = f'{clean_prefix}/{clean_name}'
205 else:
206 clean_value = sanitize_user_agent_string_component(
207 self.value, allow_hash=True
208 )
209 clean_string = f'{clean_prefix}/{clean_name}#{clean_value}'
210 if self.size_config is not None:
211 clean_string = self._truncate_string(
212 clean_string,
213 self.size_config.max_size_in_bytes,
214 self.size_config.delimiter,
215 )
216 return clean_string
217
218 def _truncate_string(self, string, max_size, delimiter):
219 """
220 Pop ``delimiter``-separated values until encoded string is less than or
221 equal to ``max_size``.
222 """
223 orig = string
224 while len(string.encode('utf-8')) > max_size:
225 parts = string.split(delimiter)
226 parts.pop()
227 string = delimiter.join(parts)
228
229 if string == '':
230 logger.debug(
231 f"User agent component `{orig}` could not be truncated to "
232 f"`{max_size}` bytes with delimiter "
233 f"`{delimiter}` without losing all contents. "
234 f"Value will be omitted from user agent string."
235 )

Calls

no outgoing calls