Skip to content

Instantly share code, notes, and snippets.

@kratsg
Created December 16, 2022 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kratsg/b6018712d66389c4a4f318d41942f9d1 to your computer and use it in GitHub Desktop.
Save kratsg/b6018712d66389c4a4f318d41942f9d1 to your computer and use it in GitHub Desktop.
import itkdb
#image = itkdb.data / "1x1.jpg"
import pathlib
image = pathlib.Path("/Users/kratsg/VBFSUSY_13_Higgsino_150.root")
import logging
import textwrap
logger = logging.getLogger('httplogger')
def logRoundTrip(response, *args, **kwargs):
extra = {'req': response.request, 'res': response}
logger.debug('HTTP roundtrip', extra=extra)
class HttpFormatter(logging.Formatter):
def _formatHeaders(self, d):
return '\n'.join(f'{k}: {v}' for k, v in d.items())
def formatMessage(self, record):
result = super().formatMessage(record)
if record.name == 'httplogger':
result += textwrap.dedent('''
---------------- request ----------------
{req.method} {req.url}
{reqhdrs}
{req.body}
---------------- response ----------------
{res.status_code} {res.reason} {res.url}
{reshdrs}
{res.text}
''').format(
req=record.req,
res=record.res,
reqhdrs=self._formatHeaders(record.req.headers),
reshdrs=self._formatHeaders(record.res.headers),
)
return result
formatter = HttpFormatter('{asctime} {levelname} {name} {message}', style='{')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.basicConfig(level=logging.DEBUG, handlers=[handler])
client = itkdb.Client(use_eos=True)
client.hooks['response'].append(logRoundTrip)
with image.open("rb") as fp:
data = {
"component": "7f633f626f5466b2a72c1be7cd4cb8bc",
"title": "MyTestAttachment",
"description": "This is a test attachment descriptor",
"type": "file",
"url": image,
}
attachment = {"data": (image.name, fp, "image/jpeg")}
result = client.post("createComponentAttachment", data=data, files=attachment)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment