EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <2024 Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <2024
<== Date ==> <== Thread ==>

Subject: Re: Use of PUT https://localhost:8181/Olog/logs/multipart REST API for creating log entries with attachments
From: Konrad Gajewski via Tech-talk <tech-talk at aps.anl.gov>
To: "Wang, Lin" <wanglin at ihep.ac.cn>
Cc: "tech-talk at aps.anl.gov" <tech-talk at aps.anl.gov>
Date: Mon, 8 Apr 2024 21:32:42 +0200
Hello Lin,

Thanks a lot, the changed proposed by you did the trick. I was so frustrated I couldn't make it work.

Best regards,
Konrad

On 4/8/2024 6:22 PM, Wang, Lin wrote:
Hello Konrad,

You may need to replace the following line,
     {"id": str(uuid.uuid4()), "name": "FREIAlogo-v3_March19.png"}
with
     {"id": str(uuid.uuid4()), "filename": "FREIAlogo-v3_March19.png"}

which means the "filename" keyword rather than "name" keyword should be used.

Attached is the test code which can upload one file successfully in my test environment.


Regards,
Lin



-----原始邮件-----
发件人: "Konrad Gajewski via Tech-talk" <tech-talk at aps.anl.gov>
发送时间:2024-04-08 19:51:29 (星期一)
收件人: "tech-talk at aps.anl.gov" <tech-talk at aps.anl.gov>
主题: [SPAM] Use of PUT https://urldefense.us/v3/__https://localhost:8181/Olog/logs/multipart__;!!G_uCfscf7eWS!c119puwJ0GJ90gvAJfVJxB2ngBCQ8UcoAcNp57Oj_ib-4-l8ahsRvrAnL7-I-tRh97q48NGBVTUP2hPoNdCndt0iB3GqTlhMY8NX$  REST API for creating log entries with attachments

Hi,

I'm trying to write a simple python program to create an entry in the
new Olog server (API documented in
https://urldefense.us/v3/__https://olog.readthedocs.io/en/latest/index.html__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhJoQQa75$  ).

I am using PUT https://urldefense.us/v3/__https://localhost:8181/Olog/logs/multipart__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhOZ21DPF$  request and
all works fine for entries without any attachments. If I try to add an
attachment the entry itself is saved but saving the attachment fails.

I have found pyOlog library but it seems to be the old version written
for Glassfish and mysql architecture.

Does anybody have a python library, or at least an example program
showing how to use the new PUT
https://urldefense.us/v3/__https://localhost:8181/Olog/logs/multipart__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhOZ21DPF$  API for creating the entries
with an attachment.  A curl command showing the use of PUT
https://urldefense.us/v3/__https://localhost:8181/Olog/logs/multipart__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhOZ21DPF$  would also be helpful.

Below is the python code that I've used:

      api_endpoint = "https://urldefense.us/v3/__https://olog-server:8181/Olog/logs/multipart__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhJIczIfa$ "

      # Prepare log entry payload
      log_entry = {
          "owner": "dev",
          "description": "New entry using Olog/logs/multipart with one
attachment.\n",
          "level": "INFO",
          "title": "test4.py",
          "logbooks": [{"name": "test"}],
          "attachments":[
              {"id": str(uuid.uuid4()), "name": "FREIAlogo-v3_March19.png"}
          ]
      }

      # Prepare the log entry payload as JSON
      json_data = json.dumps(log_entry)
      json_filename = json.dumps({"filename": "FREIAlogo-v3_March19.png"})
      json_descr = json.dumps({"fileMetadataDescription": "image/png"})

      # Prepare the multipart encoder
      multipart_data = MultipartEncoder(
          fields={
              'logEntry': ('logEntry.json', json_data, 'application/json'),
              'files': ('filename', json_filename, 'application/json'),
              'files': ('fileMetadataDescription', json_descr,
'application/json'),
              'files': ('FREIAlogo-v3_March19.png',
open('FREIAlogo-v3_March19.png', 'rb'), 'application/octet-stream')
          }
      )

      # Set the Content-Type header with the boundary
      headers = {
          'Content-Type': multipart_data.content_type
      }
      try:
          response = requests.put(api_endpoint, headers=headers,
data=multipart_data, auth=HTTPBasicAuth(username, password))
          if response.status_code == 200:
              print("Log entry created successfully.")
          else:
              print("Failed to create log entry. Status code:
{response.status_code}")
      except Exception as e:
          print("An error occurred: ",{str(e)})

And a log from phoebus-olog server:

2024-04-08 13:28:42.337 DEBUG 1740 --- [nio-8181-exec-4]
o.s.web.servlet.DispatcherServlet        : PUT "/Olog/logs/multipart",
parameters={multipart}
2024-04-08 13:28:42.347 DEBUG 1740 --- [nio-8181-exec-4]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to
org.phoebus.olog.LogResource#createLog(String, String, String, Log,
MultipartFile[], Principal)
2024-04-08 13:28:42.349 DEBUG 1740 --- [nio-8181-exec-4]
.m.m.a.RequestPartMethodArgumentResolver : Read "application/json" to
[org.phoebus.olog.entity.Log@c517dec8]
2024-04-08 13:28:42.404  INFO 1740 --- [nio-8181-exec-4]
org.phoebus.olog.LogResource             : Entry id 75 created from n/a
2024-04-08 13:28:42.406  WARN 1740 --- [nio-8181-exec-4]
org.phoebus.olog.LogResource             : File FREIAlogo-v3_March19.png
not matched with attachment meta-data
2024-04-08 13:28:42.407  INFO 1740 --- [nio-8181-exec-4]
org.phoebus.olog.LogResource             : Entry id 75 created from n/a
2024-04-08 13:28:42.408 DEBUG 1740 --- [nio-8181-exec-4]
m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json',
given [*/*] and supported [application/json, application/*+json,
application/json, application/*+json]
2024-04-08 13:28:42.408 DEBUG 1740 --- [nio-8181-exec-4]
m.m.a.RequestResponseBodyMethodProcessor : Writing
[org.phoebus.olog.entity.Log@ff3dfa85]
2024-04-08 13:28:42.410 DEBUG 1740 --- [nio-8181-exec-4]
o.s.web.servlet.DispatcherServlet        : Completed 200 OK

Best regards,
Konrad

--
Konrad Gajewski
FREIA Laboratory
https://urldefense.us/v3/__http://www.physics.uu.se/__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhPzk46FZ$









När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: https://urldefense.us/v3/__http://www.uu.se/om-uu/dataskydd-personuppgifter/__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhEJJRf56$

E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: https://urldefense.us/v3/__http://www.uu.se/en/about-uu/data-protection-policy__;!!G_uCfscf7eWS!dDgYNelpLq7ymQ7M1rIdPoepGvVQEAfQPCBtmSKeFTIOr5xieb6nu3QDLfhW7gWC1u9P52dP1-w0PfTS-T4BhX99ax7hhIzZpdTb$
VARNING: Klicka inte på länkar och öppna inte bilagor om du inte känner igen avsändaren och vet att innehållet är säkert.
CAUTION: Do not click on links or open attachments unless you recognise the sender and know the content is safe.

--
Konrad Gajewski
FREIA Laboratory
https://urldefense.us/v3/__http://www.physics.uu.se/__;!!G_uCfscf7eWS!c119puwJ0GJ90gvAJfVJxB2ngBCQ8UcoAcNp57Oj_ib-4-l8ahsRvrAnL7-I-tRh97q48NGBVTUP2hPoNdCndt0iB3GqTnzAkx2g$
References:
Use of PUT https://localhost:8181/Olog/logs/multipart REST API for creating log entries with attachments Konrad Gajewski via Tech-talk
Re: Use of PUT https://localhost:8181/Olog/logs/multipart REST API for creating log entries with attachments Wang, Lin via Tech-talk

Navigate by Date:
Prev: Check for write access in EPICS 7 Bellister, ,Jesse via Tech-talk
Next: RE: Alarm Handler Limitations Srinivas, Dhruv via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <2024
Navigate by Thread:
Prev: Re: Use of PUT https://localhost:8181/Olog/logs/multipart REST API for creating log entries with attachments Wang, Lin via Tech-talk
Next: Check for write access in EPICS 7 Bellister, ,Jesse via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  <2024
ANJ, 08 Apr 2024 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·