Jump to content
Tomasz Banas

Using data pulled via from API to JSON

Recommended Posts

I'm new to creating Workflows. I'm trying to create a workflow that will pull the data via API to JSON and show results directly in Alfred without the need to visit the website to look at the response.

 

API REQUEST

GET https://api.website.com/?domain=test.com&apikey=yourapikey&output=output_type

 

So, what I'm trying to do is this:

type "dns test.com"

 

then request is:

GET https://api.website.com/?domain=test.com&apikey=123123123123123&output=json

 

The response will be:

 

JSON response will look like this:

{
    "query": {
        "tool": "name of the tool",
        "domain": "test.com"
    },
    "expectedresponse": "205.204.123.234",
    "response": {
        "server": [
            {
                "location": "Town, Country",
                "resultvalue": "205.204.123.123",
                "resultstatus": "ok"
            },
            {
                "location": "Another Town, Another Country",
                "resultvalue": "205.204.123.234",
                "resultstatus": "ok"
            },
        ]
    }
}

 

and I'm trying to show it in the Alfred window:

 

DNS test of test.com

 

Expected Response: 205.204.70.111

1. Town, Country - 205.204.123.123 - NOT OK

2. Another Town, Another Country - 205.204.123.234 - OK

etc.

 

Any help would be appreciated :)

 

Share this post


Link to post

Using my Python library for workflows, you'd use a Script Filter and the code would look something like the below. You haven't provided enough information to actually test it, though (such as the actual API you're using), so this is an untested guess.

import sys

from workflow import Workflow3, web

log = None


def main(wf):
    query = wf.args[0]

    url = 'https://api.website.com/?domain={}&apikey={}&output=json'.format(query, apikey)
    r = web.get(url)
    r.raise_for_status()
    data = r.json()

    wf.add_item('DNS test of ' + query)
    wf.add_item('Expected response: ' + data['expectedresponse'])
    for i, d in enumerate(data['response']['server']):
        title = '{} {} - {} - {}'.format(
            i + 1, d['location'], d['resultvalue'], d['resultstatus'])
        wf.add_item(title)

    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow3()
    log = wf.logger
    sys.exit(wf.run(main))

 

Share this post


Link to post
On 6/28/2017 at 4:18 PM, deanishe said:

Using my Python library for workflows, you'd use a Script Filter and the code would look something like the below. You haven't provided enough information to actually test it, though (such as the actual API you're using), so this is an untested guess.


import sys

from workflow import Workflow3, web

log = None


def main(wf):
    query = wf.args[0]

    url = 'https://api.website.com/?domain={}&apikey={}&output=json'.format(query, apikey)
    r = web.get(url)
    r.raise_for_status()
    data = r.json()

    wf.add_item('DNS test of ' + query)
    wf.add_item('Expected response: ' + data['expectedresponse'])
    for i, d in enumerate(data['response']['server']):
        title = '{} {} - {} - {}'.format(
            i + 1, d['location'], d['resultvalue'], d['resultstatus'])
        wf.add_item(title)

    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow3()
    log = wf.logger
    sys.exit(wf.run(main))

 

 

Here's the workflow - http://d.inco.re/mYKH6

Share this post


Link to post

Deanishe, you're right. I'm not sure what to do, where to add my API key, etc. I created "script filter" but still don't know what I'm doing wrong. Do you mind uploading the final forkflow? as of the installation Alfred says "If you intend to distribute your workflow to other users, you should include Alfred-Workflow (and other non-standard Python libraries your workflow requires) within your workflow as described above. Do not ask users to install anything into their system Python. That way lies broken software." So, I guess I should not to force someone to install anything. 

 

I appreciate your help.

Share this post


Link to post
8 hours ago, Tomasz Banas said:

where to add my API key

 

Put it in the workflow configuration sheet. If you call it, say, API_KEY, you can get it from a Python script with api_key = os.getenv('API_KEY'). This is described in the stickied thread on workflow variables.

 

8 hours ago, Tomasz Banas said:

Do you mind uploading the final forkflow?

 

i don't have a final workflow. As I said, I just wrote that script without testing it. I'm happy to answer any questions, but I'm not going to repeat what's clearly described in the library's documentation.

 

8 hours ago, Tomasz Banas said:

as of the installation Alfred says "If you intend to distribute your workflow to other users, you should include Alfred-Workflow (and other non-standard Python libraries your workflow requires) within your workflow as described above.

 

So, the answer is literally on the same page. And, the tutorial describes a workflow very similar to what you're trying to do. With the script I posted above and the rest of the library docs, you should be able to get very close to what you're trying to achieve.

 

I'd be happy to look over what you come up with if there are any problems.

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...