uhd-urz / elAPI

An extensible API client for eLabFTW
GNU Affero General Public License v3.0
5 stars 0 forks source link

Proper URL support - [closed] #82

Closed alexander-haller closed 2 months ago

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 5, 2024, 17:58

Merges improve-url-class -> dev

This PR adds proper URL support for making all kinds of API requests via elAPI. Previously, elAPI only supported the following URL pattern: https://elabftw-dev.uni-heidelberg.de/api/v2/<endpoint name>/<endpoint id>.

This PR adds a ElabFTWURL class that supports a more complete URL. We also introduce the following terms to identify certain parts of a URL.

image

endpoint_id, sub_endpoint_name, sub_endpoint_id, query are optional. We can pass these parts as arguments to ElabFTWURL, and its get() method will simply convert them to a full URL for us. E.g.,

>>> url = ElabFTWURL(endpoint_name="experiments", experiment_id=17)
>>> url.get()
https://elabftw-dev.uni-heidelberg.de/api/v2/experiments/17
>>> url = ElabFTWURL(endpoint_name="experiments", experiment_id=17, query="{"format": "pdf"}")
>>> url.get()
https://elabftw-dev.uni-heidelberg.de/api/v2/experiments/17?format=pdf

This prepares elAPI for more advanced plugins like the upcoming experiments plugin that uses the sub_endpoint_name and query to download attachments. We can also use query to perform advanced tasks like using the search functionality within eLabFTW.

ElabFTWURL also does a weak validation for the endpoint and sub-endpoint names that are passed.

>>> url = ElabFTWURL(endpoint_name="experiment", experiment_id=17)  # Notice the typo in "experiment" instead of "experiments"
>>> url.get()
ValueError: Endpoint must be one of valid eLabFTW endpoints: ...

Note: eLabFTW uses the terms "entity" and "endpoint" to refer to endpoint_name and "models" to refer to "sub_endpoint_name". I don't think the terms "entity" and "models" make it clear in what they mean. To avoid confusion, I have decided to use generic terms like "endpoint_name" and "sub_endpoint_name".

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 5, 2024, 17:58

requested review from @alexander-haller

alexander-haller commented 6 months ago

I love how you create merge requests. They are succinct, contain examples and give context.

alexander-haller commented 6 months ago

approved this merge request

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 6, 2024, 24:12

I'm glad you liked it! They also serve as documentations for me. In fact, I meant to use PR details and our discussions here as the basis of documentation (proper documentation) which we currently don't have yet.

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 6, 2024, 24:13

Thanks for approving!

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 6, 2024, 24:20

Looks like I made a terrible mistake with this branch. I ran a git-filter-repo command from SO without realizing it affects all past commits, and it essentially rewrote every commit. Which is why this branch has 516 commits! It should have less than 10.

Edit: Fixed! At least the remote repo. That was close!

alexander-haller commented 6 months ago

In GitLab by @project_994_bot_1f89ebd5af89f5e987c71c650dd64895 on Mar 6, 2024, 24:40

added 516 commits

Compare with previous version

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 6, 2024, 03:01

resolved all threads

alexander-haller commented 6 months ago

Tell me how you fixed it so that I learn this! And also sorry that I did not pay notice to the large # of commits when looking over it.

alexander-haller commented 6 months ago

In GitLab by @mhxion on Mar 6, 2024, 16:16

And also sorry that I did not pay notice to the large # of commits when looking over it.

That's totally fine. I myself only noticed it right before when I was trying to merge it and GitLab was showing a merge conflict. And I was like "what conflict?!..." 🤦🏼‍♂️

Tell me how you fixed it so that I learn this!

You're probably going to enjoy my naive process:

  1. git-filter-repo corrupted my entire local repository in directory ~/Workshop/elapi including all local branches and the git stash.
  2. I cloned our remote repo to ~/Workshop/elapi-tmp/. The remote repo's main and dev branches remained unaffected ofc. cded to elapi-temp/.
  3. I recreated the branch improve-url-class (the same name that was on the corrupted branch) from branch dev. This branch was uncorrupted but it didn't have the new commits from the corrupted branch improve-url-class from the corrupted repo.
  4. I followed the steps here to get just the new commits previously made to the corrupted branch.
  5. This improve-url-class branch now remained uncorrupted, without any change in commit history that git-filter-repo rewrote with just the commits I needed. I force pushed this branch. Because the name of the branch was same, GitLab just updated the corrupted improve-url-class branch. The PR showed the correct number of commits. Everything was back to normal.
  6. I moved the good .git folder from ~/Workshop/elapi-tmp/ to ~/Workshop/elapi, so I could keep working on my old local repository where all the virtual envs were.
  7. To get my stash back where I did a week-worth of work, I resorted to PyCharm's "local history". And this is a really awesome feature of PyCharm where it saves almost every single change made in the past just like Git but automatically without having to commit anything. If I had deleted the corrupted directory ~/Workshop/elapi and started fresh I'd have lost this local history from PyCharm. I found another way to get deleted stash back from Git itself, but I never tried it.
alexander-haller commented 6 months ago

In GitLab by @project_994_bot_1f89ebd5af89f5e987c71c650dd64895 on Mar 7, 2024, 03:00

added 12 commits

Compare with previous version

alexander-haller commented 6 months ago

In GitLab by @project_994_bot_1f89ebd5af89f5e987c71c650dd64895 on Mar 7, 2024, 03:02

added 12 commits

Compare with previous version

alexander-haller commented 6 months ago

In GitLab by @project_994_bot_1f89ebd5af89f5e987c71c650dd64895 on Mar 7, 2024, 03:03

added 12 commits

Compare with previous version

alexander-haller commented 5 months ago

In GitLab by @mhxion on Mar 22, 2024, 14:14

resolved all threads

alexander-haller commented 5 months ago

In GitLab by @mhxion on Mar 22, 2024, 14:15

This PR had already been merged to https://gitlab.urz.uni-heidelberg.de/urz-elabftw/elapi/-/merge_requests/32. But I guess I never merged the PRs systematically. My bad! This PR can be safely closed now, though the branch is outdated and shouldn't be used.