Project Idea 03 - Write a Trello Library

itachiuchiha

Itachi Uchiha

Posted on May 17, 2020

Project Idea 03 - Write a Trello Library

This post published on my blog before

Hi everyone. Today, I'm going to talk about my new project idea. (Actually, there is no new thing).

I know, most developers don't want to spend time to create a library for Trello. Because there must already be a good library.

What will we learn by doing this project?

The main idea is that we can understand how API calls works. For example, when should I use PUT or GET request.

In this project, we'll work with different HTTP verbs. This will show us why and when we should use that verbs.

What is Trello?

If you're new in project management tools, Trello helps you to organize your tasks using boards and cards. There are also useful plugins, etc.

You create a task and assign it to a member of your team. You can assign yourself.

For example, I'm using Trello to organize my daily tasks. This post one of them. You can see my private To-Do List. There are also shopping boards, travel boards as private boards.

My Trello Board

In short, Trello a tool to project management.

Which Programming Language I Should Use?

Actually, programming language only a tool. Your idea is more important than that.

But I can suggest popular languages like Python, Go, Ruby, Rust, etc.

How to Access Trello Developer Portal

You can access the Trello REST API portal using this url.

What Do We Need?

We must have an app key and OAuth token. In fact, you can find them on the same page.

To get app key use this url. The key is app key. To get OAuth token for local testing, you should click Token url.

Trello app key

There is an OAuth secret key at the bottom of the page.

Some Basic Endpoints

REST API calls will return a JSON output.

Member Boards

If you want to get your board you have to use me key. If you want to see a different user's board you should use a username.

My boards

curl https://api.trello.com/1/members/me/boards?key=APP_KEY&token=OAUTH_TOKEN
Enter fullscreen mode Exit fullscreen mode

John Doe's boards

curl https://api.trello.com/1/members/johndoe/boards?key=APP_KEY&token=OAUTH_TOKEN
Enter fullscreen mode Exit fullscreen mode

These requests are GET requests.

See all APIs for Boards

Lists

To get the list information, you should use this endpoint.

curl https://api.trello.com/1/lists/{LIST_ID}?key=APP_KEY&token=OAUTH_TOKEN
Enter fullscreen mode Exit fullscreen mode

This request is another GET request.

Cards

To get card information, you should use this endpoint.

curl https://api.trello.com/1/lists/{CARD_ID}?key=APP_KEY&token=OAUTH_TOKEN
Enter fullscreen mode Exit fullscreen mode

These are some endpoints. But if you take a look at API reference documentation, there are many fields to customize your requests.

You can use this url

Rate Limits

You have to respect rate limits. Check this document

To help prevent strain on Trello's servers, our API imposes rate limits per API key for all issued tokens. There is a limit of 300 requests per 10 seconds for each API key and 100 requests per 10 second interval for each token. Additionally, there is a limit on requests to /1/members/ of 100 requests per 900 seconds. If a request exceeds the limit, Trello will return a 429 error along with a message corresponding to which limit was exceeded.

What Should We Do?

First of all, we can create classes for each feature. For example, I created a class called boards

boards.ts

import ApiUrls from "../http/constants";
import BASE_URL, { ApiBase } from "../http/api";
import request from "../http/request";

import "../util/format";
import { AxiosResponse, AxiosError } from "axios";

class Boards extends ApiBase {
    private static boardUrl: string;

    public static boards(username: string) {

        this.boardUrl = ApiUrls.boards.format(username) + this.token

        return this
    }

    public static fields(fields?: string) {
        if (fields) {
            this.boardUrl += `&fields=name,url`
        }

        return this
    }

    private static apiUrl() {
        return BASE_URL + this.boardUrl
    }

    public static get(): Promise<any> {

        const url = this.apiUrl()

        return new Promise((res, rej) => {

            request({
                method: 'get',
                url: url
            })
            .then((response: AxiosResponse) => {

                const { data, headers } = response

                res({ data, headers })
            })
            .catch((error: AxiosError) => {

                if (error.response!.status === 429) {
                    rej({
                        code: 429,
                        message: 'Rate limit exceeded'
                    })   
                }
            })

        })
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, I'm going to another class called Trello

trello.ts

import { ApiBase } from "./http/api";

import Boards from "./features/boards";



class Trello {

    constructor(appKey: string, apiKey: string) {

        ApiBase.auth(appKey, apiKey)
    }

    get instance() {

        return {
            Boards,
        }
    }
}

export {
    Trello
}
Enter fullscreen mode Exit fullscreen mode

And I'm going to create index file.

index.ts

import { Trello } from "./trello";

const trello = new Trello(APP_KEY  OAUTH_KEY).instance

const myBoards = async () => {

    const api = trello.Boards

    const userBoards = api.boards('me').fields('name, url')

    const { data, headers } = await userBoards.get()

    console.log(data)
}
Enter fullscreen mode Exit fullscreen mode

This was a sample code. You can change your logic. Actually, my sample codes don't look good.

EOL

I think we have a good library now.

We can use it in our projects or publish it to package manager stores.

Sorry for the grammar mistakes.

Thanks for reading ^_^ and if there is something wrong, tell me.

Resources

💖 💪 🙅 🚩
itachiuchiha
Itachi Uchiha

Posted on May 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related