Art-of-WiFi / UniFi-API-browser

Tool to browse data exposed by Ubiquiti's UniFi Controller API
MIT License
1.1k stars 150 forks source link

Supply URL to fetch json directly #77

Closed rayflexcom closed 4 years ago

rayflexcom commented 4 years ago

Hi, first of all, great tool!

I was wondering, when I open for example the "list devices" JSON i can copy the JSON, but is there also a URL available somewhere for the selected output which i can use to download the JSON directly?

For example https://Unifi-API-browser.ext?siteid=520942389243120&collection=listdevices which then wil output the RAW JSON output so I can read it out with another application?

malle-pietje commented 4 years ago

Hi (fellow Dutchman), This feature has been asked several times before, and while I understand the use-case it is not what the tool was intended for (a "discovery" tool for developers). For your example, it is actually quite easy to create a custom script on the same server as where you installed the API browser tool that does this for you.

You can use this code example from the PHP API client as a starting point: https://github.com/Art-of-WiFi/UniFi-API-client/blob/master/examples/list_connected_users.php

With one or two changes you are done.

rayflexcom commented 4 years ago

Hi Pietje,

Thank you, then I know I did not miss something and have to set it up myself ;)

Cheers

malle-pietje commented 4 years ago

OK, good luckšŸ‘ Feel free to post an issue in the PHP API client repo if you run into any questions.

nodecentral commented 3 years ago

Hi @malle-pietje

I really love this tool, but Iā€™d also like to be able to access the json directly via a http call. I can see there is a lot of discussion around this on the forums, and unifi-api-client is mentioned , but I guess Iā€™m curious if something using the sort of url that @rayflexcom suggested is possible?

If not, Iā€™m running the Docker version of the API Browser, and I can see you have suggested creating custom scripts above , butnot being a programmer - how easy is that to do with the container set up ?

Huge thanks in advance..

malle-pietje commented 3 years ago

@nodecentral Thanks, glad you like it. As explained earlier, this feature is not going to be implemented unless someone comes up with a PR. It just doesn't fit with the usage we have in mind. It is quite easy (even for people with basic PHP skills) to create such a script yourself.

malle-pietje commented 3 years ago

See my comment that refers to the code example from the PHP API client repo.

nodecentral commented 3 years ago

Thanks so much for responding @malle-pietje

Iā€™m up for using the API client if that will do the same thing, but Iā€™m not sure how that work with the Docker setup ?

If you could point me in the right direction - steer me the right way - Iā€™m more than happy to give it a go..

Am I correct in saying I need to put the (example) files you created here (https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples) into a specific location on the UniFi API Browser docker ?

And then to get the json to download, i could call the file name via a url ? e.g..

192.168.22.227:8000/list_connected_users.php

BTW - Whatā€™s a PR ?

malle-pietje commented 3 years ago

PR = Pull Request (proposed changes to the code within this repository)

Your assumptions regarding the code are correct. Make sure to place your custom script somewhere "under" the webroot of your PHP-enabled web server. Also, make sure to restrict access to these scripts because they won't require authentication.

The scripts should return json using something like this at the end of your logic after the data has been successfully pulled from the API (and post-processed if needed):

/**
 * output the results with correct JSON formatting
 */
header('Content-Type: application/json');
echo json_encode($results, JSON_PRETTY_PRINT);
nodecentral commented 3 years ago

Thanks again @malle-pietje

Regarding your comment ..

Make sure to place your custom script somewhere "under" the webroot of your PHP-enabled web server. Also, make sure to restrict access to these scripts because they won't require authentication.

As Iā€™m using the Docker image associated with UniFi Browser API, where would I find webroot ? If it helps, I can access the command line of the Docker container via this..

docker exec -it unifi-host /bin/ash

Once in, I can see the following -

/ # ls
UniFi-API-browser  bin  config  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  srv  start.sh  sys  tmp  usr  var
/ # cd UniFi-API-browser
/UniFi-API-browser # ls
LICENSE.md  README.md  TODO.md  ajax  collections.php  common.php  composer.json  composer.lock  config  css  favicon.ico  index.php  js  login.php  logout.php  templates  vendor
/UniFi-API-browser # 

Would I create / download those custom scripts files into UniFi-API-browser folder (as above)?

malle-pietje commented 3 years ago

Yes, you could place them there. Keep in mind when updating the API Browser not to lose your custom work...

nodecentral commented 3 years ago

Great ! (Thanks so much)

Ok, now, letā€™s see if I can just download them straight in...

Checking the Docker, I donā€™t seem to have the git app installed, but I do have wget. Please could you help with the wget command to pull down those scripts in a tar or zip file (https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples) ? E.g

wget https://github.com/<user>/<repo>/archive/<branch>.zip
malle-pietje commented 3 years ago

You can simply copy the examples from the vendor directory. They are already bundled with the API Browser tool through the included PHP API client.

nodecentral commented 3 years ago

You can simply copy the examples from the vendor directory. They are already bundled with the API Browser tool through the included PHP API client.

Thanks so much, thatā€™s even easier :)

As for next steps - and correct me if Iā€™m wrong - I now need to follow the client instructions part now and update the template file etc. ?

Out of curiosity, (not being a .php person) are you able to just run the files from that vendor location?


http://192.168.22.227:8000/vendor/art-of-wifi/unifi-api-client/examples/list_connected_users.php
nodecentral commented 3 years ago

Iā€™m progressing - Iā€™ve updated the config.php - but each one I call returns false

As this relates to the api-client, Iā€™ll open a ticket there.

Thanks for all your help so far ...