API search list user by array of user names


(Tranquyet) #1

Hi everyone,

My website has integrated with Discourse forum, users mapping by username. I would like to have an user reports with some fields like this:

  • Username
  • Posts count
  • Likes count

I found an API that could work: GET /admin/users/list/{flag}.json
But this API is missing “Likes count”, and can not search by a concatenate string of user names like this: username1,username2,username3

Currently i have to fetch one by one, but it’s very slow. We are having more than 10000 users.
It would be great if there is a way to search be a concatenate string of user names.

Any help would be very appreciate.

Thanks


(Vinoth Kannan) #2

Try Data Explorer Plugin.


(Tranquyet) #3

Thanks @vinothkannans. But in my case, i need to do some filters in our user dataset in my website first, then call Discourse API to get user data from Discourse forum. So i think it would be fine if there is an api like this:
GET /admin/users/list/{flag}.json?usernames=username1,username2
Of course the response data should include Likes count.


(Vinoth Kannan) #4

Read the instructions. It’s already possible in the plugin. You can filter users by username using params

-- [params]
-- null string_list :usernames

SELECT * FROM users WHERE username IN (:usernames)

(Tranquyet) #5

Thanks @vinothkannans. Now i can use Data Explorer plugin to build my query. When i try to use Postman to run the query, it works fine. But it did not work with PHP Guzzle HTTP. I turn on guzzle http debug option, here is my debug info:

* Hostname was found in DNS cache
* Hostname in DNS cache was stale, zapped
*   Trying 40.69.218.34...
* Connected to mydomain.com (myip) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* 	 subject: CN=mydomain.com
* 	 start date: 2018-05-05 23:48:11 GMT
* 	 expire date: 2018-08-03 23:48:11 GMT
* 	 subjectAltName: mydomain.com matched
* 	 issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
* 	 SSL certificate verify ok.
POST /admin/plugins/explorer/queries/1/run HTTP/1.1
Host: mydomain.com
Content-Type: application/x-www-form-urlencoded
User-Agent: MyUserAgent
Accept-Encoding: *
Content-Length: 139

* upload completely sent off: 139 out of 139 bytes
HTTP/1.1 406 Not Acceptable
* Server nginx is not blacklisted
Server: nginx
Date: Thu, 28 Jun 2018 17:23:54 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
X-Request-Id: 701b309c-83e7-45b2-a47c-f002bbd17031
X-Runtime: 0.011804
* Connection #0 to host mydomain.com left intact```

I got 406 HTTP Not Acceptable. I tried add/not set User-Agent, Accept-Encoding ... to request header. But nothing works. And here is my PHP CODE: 

$response = $this->client->post('admin/plugins/explorer/queries/1/run', [
                'form_params' => [
                    'api_key' => $api_key,
                    'api_username' => 'system',
                    'params' => '{"usernames":"quyettran"}'
                ],
                'debug' => true
 ]);

Everyone help. Thanks

(Vinoth Kannan) #6

You have to use Content-Type: application/json.


(Tranquyet) #7

Changed to Content-Type: application/json. Now I get HTTP 404 status code. Still can not figure out the reason. In the meantime, i used Curl. It works fine.
Thanks for your help.