Rooyca / obsidian-api-request

Obsidian plugin that allows you to make HTTP requests and display responses directly in the document, in codeblocks, or store them in localStorage.
https://rooyca.github.io/obsidian-api-request/
MIT License
100 stars 5 forks source link

[Question] Iterating on Array in Configured API Request #15

Closed julroger2013 closed 5 months ago

julroger2013 commented 5 months ago

I had a question about the configured API requests.

I have a codeblock formatted as so:

url: https://api.todoist.com/rest/v2/tasks?filter=`contentOfFilter`
method: get
headers: { "Authorization": "Bearer myToken"}
show: {..} -> content

Todoist returns an array of JSON objects, with each object representing a task, with various components such as id, content, priority, and so on.

This codeblock works perfectly in extracting the "content" component of each JSON object in the array - functionally, it's grabbing the name of each task that Todoist sends back.

I'm finding that I cannot figure out how to obtain the same results with the preconfigured API requests that you can set up in the plugin settings.

I would imagine that limiting to something like "content" - one component of each object in an array of objects - is done with the "What to display" box in the plugin settings, but I can't seem to figure out exactly how to set up the preconfigured API requests to obtain the same result as the codeblock above.

Any ideas on how I need to structure this configured request to obtain the desired result?

Thank you!

Rooyca commented 5 months ago

Hey @julroger2013! In the last update I was planning to remove that part of the plugin, it seemed to me that it didn't add much and was a bit convoluted. Glad to hear someone is using it.

The short answer to your question is: You can't. The codeblock part and the configuration part are two separate parts.

If it's not too forward I would like to know why not to use codeblocks?

julroger2013 commented 5 months ago

Good question. Maybe you can help me figure out how to achieve the desired result I have in mind, whether that uses codeblocks or configured API requests.

I manage my tasks with Todoist, which has a pretty well-established REST API.

What I'm wanting to do is pull a list of tasks from Todoist in markdown format using the API. Each task is a JSON object with many components, such as priority, due date, UID, task name.

I'd like to pull tasks using a filter function built into the API, and then extract only the task names from the returned JSON objects.

I would want to format these as a markdown checklist, e.g. ...

And so on. That list then goes through a script (which I already have set up through Templater) which pairs the tasks against each other head-to-head (i.e. 1 v. 2, 1 v. 3, 2 v. 3, etc.) to prioritize. The task that ends up with the most head-to-head victories ends up first priority, second most as second priority, and so on.

Basically, I'm just trying to use your plugin to manage the first part of this workflow - extracting the tasks from Todoist's API and putting them into a markdown checklist that can be processed by the script that I've already established. I figured I could use the "Paste Result" command from your plugin to produce that list in one step.

Does that make sense? I'm open-minded about how best to approach this problem.

I'm trying to get as close as I can to a single button-press to extract tasks, format them in a markdown checklist, and run them through the prioritization process.

Thank you!

Rooyca commented 5 months ago

I think I'll have to change or add a couple of things on the plugin for this to work the way you need to.

When I find a solution I'll write you.

Rooyca commented 5 months ago

I found a 'easy' way of doing this:

url: https://api.todoist.com/rest/v2/tasks
headers: {"Authorization": "Bearer TOKEN"}
show: {..} -> content
format: <input type="checkbox"> {} </input> <br>

Do you think that would work with your workflow?

julroger2013 commented 5 months ago

The format is pretty much right. The one downside is that it's not possible to drag-select the checkboxes, too, for copy-paste purposes. That's why I hoped to instead format things in a configured request and paste them with a command.

As I'm sitting here thinking about it, though ... Templater can run JavaScript - is there any reason I can't just do an API request as part of a Templater script and then use other JavaScript functions as part of that script to format the returned values?

Thanks for thinking this over!

julroger2013 commented 5 months ago

One possibility - would it be hard to add a "copy contents" button to the top right corner of your code blocks? They have those in ordinary code blocks & also optionally in callouts, so you can copy the contents of the block. That might work well, and maybe would be easier than the configured portion of your plugin?

Thanks again!

Rooyca commented 5 months ago

Templater can run JavaScript

I believe you could do that. Don't know if it works tho.

add a "copy contents" button

I did that. Would be live in the next update. For this to work you should remove the <input> an replace it with a - [ ].

url: https://api.todoist.com/rest/v2/tasks
headers: {"Authorization": "Bearer TOKEN"}
show: {..} -> content
format: - [ ] {} <br>
julroger2013 commented 5 months ago

Great, thank you!

A marginal idea, since the main benefit will already come from the copy contents function, but do you suppose your code blocks could be made to render markdown?

The Better Search Views plugin probably has useful code when it comes to markdown rendering. It makes markdown render in, eg, backlinks views.

Thanks again for adding the copy contents button! That will get me where I need to go.

Have a great day.