How to get the full list of all Hive communities using Bridge API and Node.js/JavaScript

avatar

The purpose of this tutorail is to share a way you can easily return a full list of all Hive communities using the Bridge API.

If you are a dev taking a look at the Hive Blockchain API docs specifically the list_communities operation, you will find that it returns the list of communities in batches of 100(for pagination sake). This is actually a good way to return a list of communities in a manner that is not cumbersome.

However, some situations might warrant the need to return a full list of all communities for use. An example would be displaying the list communities in a select input like in the image below

In order to achieve this, we would need to make multiple calls to the list_communities API until it has retruned the full list of all the communities.

Remember that the API call only returns only 100 communities per call. Each call returns a list based on the arguments that are provided.

There are five arguments that can to make an API call to get list of communities. They are all optional.

Let's examine each of them in detail.

last

last is an optional string value that takes the name of the community that will serve as the starting point for the extraction and return of the next 100 communities after it on the list.

limit

limit takes an integer as value and it specifies the number of communities that should be returned in the requests response. It takes a default value of 100.

query

If we need to return communities that is related to a specific word/phrase or group of words, those words would have to be passed as the value of query. Like others it is optional.

sort

This argument helps to sort the arrangement of the returned communities. sort can take any of the following values each one affecting the lineup of the returned data.

  • rank: This value will return a list of communities starting from the most popular
  • new: This will return a list of communities starting from newly creted communities
  • subs: This will return a list of communities that the observer account is subscribed to

observer

This is also an optional argument that takes the username of a Hive account. This is useful when we need to return details about communities associated with that account either as an admin or a subscriber.

Now that we are done explaining the values need to make the API call, let us visit the API call itself.

Remember that all of the arguments above are optional, so if you don't see them in the code or if one or two is missing that does not devalidate the code.

To return a list of communities, the request body to the Bridge API would look something like this

{"jsonrpc":"2.0", "method":"bridge.list_communities", "params":{"limit": 1, "query": "wall street bets"}, "id":1}

The API url can be the link to any Hive node, but for this post we will be using api.hive.blog.

We will also be using axios to send the API call and recieve a response. You can install axios by running npm install axios --save in terminal of the project directory.

A full API call for returning the list of communites would be

axios.post(
            'https://api.hive.blog', 
            JSON.stringify({
                "jsonrpc":"2.0", "method":"bridge.list_communities", "params":{"limit": 100, sort: 'rank'}, "id":1
            })
        ).then(res => {response code here}).catch(err => {error handler here})

or in an async/await context

const communities = await axios.post(
            'https://api.hive.blog', 
            JSON.stringify({
                "jsonrpc":"2.0", "method":"bridge.list_communities", "params":{"limit": 100, sort: 'rank'}, "id":1
            })
        )

The axios call above returns a list of first 100 communities based on rank. That's fine however that is not what we want. What we truly want is for it to return a list of all communities on Hive(and there is over 3000 of them).

We would have to create a loop that runs until all communities have been retreived and stored in an array. This array is what would be returned as response.

To achieve this we would need two API calls, the first one to load the initial 100 communities and the second one to run in a loop to load the remainder.

The condition for our loop will be for it to keep running as long as the returned array of the last sent request has a length greater than zero, if the response for the last request returns zero it means the last request did not get any list of communities to return which lso means that we have gotten the full list of communities as we wanted so now we just have to return it as a response.

Below is how the function for returning all communities on Hive at once would look like

getAllCommunities: async function () {
        let popularCommunities = []

        let commies = []

        let initCommiesArr = await axios.post(
            'https://api.hive.blog', 
            JSON.stringify({
                "jsonrpc":"2.0", "method":"bridge.list_communities", "params":{"limit": 100, sort: 'rank'}, "id":1
            })
        )

        let initCommies = initCommiesArr.data.result

        commies = commies.concat(initCommies)
        let lastComm = initCommies[initCommies.length - 1]
        
        async function retrieveCommunities(indexName) {
            let getCommiesArr = await axios.post(
                'https://api.hive.blog', 
                JSON.stringify({
                    "jsonrpc":"2.0", "method":"bridge.list_communities", "params":{"last": indexName, "limit": 100, sort: 'rank'}, "id":1
                })
            )
    
            let getCommies = getCommiesArr.data.result

            return getCommies
    
        }

        let theComms = await retrieveCommunities(lastComm.name)
        commies = commies.concat(theComms)
        do {
            lastComm = theComms[theComms.length -1]
            theComms = await retrieveCommunities(lastComm.name)
            commies = commies.concat(theComms)
        }
        while (theComms.length > 0)

        const communities = commies
        return communities;
  }

In the function above, initCommiesArr is the API call that returns a response containing the initial 100 communities. This list of communities is then stored in initCommies after which we extract the last community in the array and store it in lastComm.

The value of lastComm is then passed to retrieveCommunities() which is coded to run for as long as our condition holds true.

The function above would return an array of communities similar to the one below which we can proceed to use for whatever we had in mind.

That concludes it for this tutorial, I hope you found this piece useful. Kindly share your thoughts and comments in the comment section. Thanks for reading, please do not forget to upvote and like.

Posted from HypeTurf



0
0
0.000
0 comments