Created
December 16, 2022 16:31
-
-
Save kratsg/b6018712d66389c4a4f318d41942f9d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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