MaxIV-KitsControls / Elogy

GNU General Public License v3.0
7 stars 1 forks source link

Creating entries via http post #3

Closed dschick closed 5 years ago

dschick commented 5 years ago

Hej,

I am currently able to get entries as described in the readme file as http localhost:4000/api/logbooks/2/entries/1/ I use the result of that and save it to a file named new_entry.json. In this file I just include the minimum of information which might be necessary to create a new entry new_entry.json:

"entry": {
    "title": "my new entry via http",
    "authors": [
        {
            "name": "root",
            "login": "root",
            "email": null
        }
    ],
    "attributes": {},
    "attachments": [],
    "priority": 0,
    "metadata": {},
    "content": "<h1>hello world</h1>",
    "content_type": "text/html",
}

Then I post it via http localhost:4000/api/logbooks/2/entries/ < new_entry.json This will actually create a new entry but without any of the above mentioned information/content, neither, title, author, nor content are included in the created entry.

Is there maybe already any library available to work with elogy e.g. from python?

Best

Daniel

johanfforsberg commented 5 years ago

Hi Daniel, thanks for trying this out! I think what you need to do is to remove the initial '"entry":' in your file, and just include the object. I can't try this right now and the documentation is definitely lacking, but you might get some more clues on how the api works of you look in the tests in "back end/test/test_api.py".

dschick commented 5 years ago

great that is working. it seems that there was also some problem with the json encoding.

johanfforsberg commented 5 years ago

Great! To adress your other question, while I think it makes some sense to have a python client for elogy, in the end it would probably just be a very small wrapper for "requests" or something. I think it's probably enough to just make it easy to do using curl or whatever tool you're comfortable with, as it's a fairly simple http api.

dschick commented 5 years ago

hej @johanfforsberg , I actually used requests. Here is a basic script which allows to create entries to a specific logbook:

import json
import requests

host = 'http://localhost:4000/'

def post_json(url, data, **kwargs):
    putdata = data#json.dumps(data)
    print(putdata)
    print(url)
    return requests.post(url, json=putdata, **kwargs)

def decode_response(response):
    return json.loads(response.get_data().decode("utf-8"))

def make_logbook(client, data=None):
    in_logbook = data or dict(
        name="Test logbook",
        description="Test description")
    response = client.post("/api/logbooks/", data=in_logbook)
    assert response.status_code == 200
    return in_logbook, decode_response(response)["logbook"]

def make_entry(logbook, data=None):
    in_entry = data or dict(
        title="Test entry from Daniel",
        authors= [dict(name="root", login="root",)],
        content="This is some test content!",
        content_type="text/plain")
    response = post_json(
        "http://localhost:4000/api/logbooks/{logbook[id]}/entries/".format(logbook=logbook),
        data=in_entry)
    print(response)
    #assert response.status_code == 200
    #return in_entry, decode_response(response)["entry"]

then you can simply call: make_entry({'id': 1}) to make an entry into the logbook with ID:1

Tack so mycket

johanfforsberg commented 5 years ago

Cool, thanks! We should add this to the documentation, it's really lacking right now.