Jump to content

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 :)

 

Link to comment

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))

 

Link to comment
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

Link to comment

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.

Link to comment
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.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...