NeboWiki
NeboWiki

Reputation: 159

How to the Trello API Batch method

I am learning the Trello API and for the most part it is not difficult. However, the batch method, for batching GET requests has real potential to minimize traffic and so on. However, I can't get it to work. It always complains about an invalid token. Though if I append the token in the GET URL, it doesn't seem to matter.

Anyone have a working batch example? (simple URL string that works in the browser?)

Thanks!

Upvotes: 0

Views: 1180

Answers (2)

Martin Cronje
Martin Cronje

Reputation: 467

For those people who use Python

Using the request library:

urls = []
for card in cards[0:5]:
    urls.append(f"/cards/{card['id']}/attachments")

query = {
    'urls' : urls,
    'key' : trelloKey,
    'token' : trelloToken
}
response = requests.request("GET", "https://api.trello.com/1/batch", params=query)

Using the python trello library

documentation : https://pythonhosted.org/trello/trello.html

board_id = ""
cards = trello.boards.get_card(board_id )

urls = []
for card in cards[0:5]:
    urls.append(f"/cards/{card['id']}/attachments")

response = trello.batches.get(urls)

The batches API documentation (https://developer.atlassian.com/cloud/trello/rest/api-group-batch/#api-group-batch) doesn't show how the urls should be formatted.

The above only gets the attachements of the first 5 cards. According to the documentation you can batch 10 requests at a time.

This will help get all the attachments:

from trello import TrelloApi
trello = TrelloApi(trelloKey, trelloToken)  
urls = []
attachments = []
for pos, card in enumerate(cards):
    pos = pos + 1
    urls.append(f"/cards/{card['id']}/attachments")
    if pos % 10 == 0 or pos == len(cards):
        attachments += trello.batches.get(urls=urls)
        urls = []

Upvotes: 1

James
James

Reputation: 53

This is working for me...

https://api.trello.com/1/batch?urls=/members/me/boards,/members/me&key=YOUR_KEY&token=YOUR_TOKEN

That's to GET from '/members/me/boards' and '/members/me' at the same time.

It's a bit easier to let Client.js log in and get the data for you (it gets the key and token for you too). Try this...

// Call this function
function trelloBatchTest() {

    // Try to log into Trello before getting data
    if (Trello.authorized() === false) {
        Trello.authorize({
            type: "popup",
            interactive: true,            
            scope: { read: true, write: true, account: true },
            success: function () {
                getBatchData();
            },
            error: function () {
                console.log("error logging in");
            }
        });
    }

    // Get data straight away if already logged in
    else {
        getBatchData();
    }         
}

// Makes a batch GET request to Trello - called from function above
function getBatchData() {
    Trello.get("/batch?urls=/members/me/boards,/members/me", function(data) {
        console.log(data);
    }, function (error){
        console.log(error);
    });
}

Hope that helps :)

Upvotes: 1

Related Questions