RestAPI.qml 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import QtQuick
  2. import Felgo
  3. Item {
  4. // loading state
  5. readonly property bool busy: HttpNetworkActivityIndicator.enabled
  6. // configure request timeout
  7. property int maxRequestTimeout: 5000
  8. // initialization
  9. Component.onCompleted: {
  10. // immediately activate loading indicator when a request is started
  11. HttpNetworkActivityIndicator.setActivationDelay(0)
  12. }
  13. // private
  14. QtObject {
  15. id: _
  16. property string todoUrl: "https://jsonplaceholder.typicode.com/todos"
  17. function fetch(url, success, error) {
  18. HttpRequest.get(url)
  19. .timeout(maxRequestTimeout)
  20. .then(function(res) { success(res.body) })
  21. .catch(function(err) { error(err) });
  22. }
  23. function post(url, data, success, error) {
  24. HttpRequest.post(url)
  25. .timeout(maxRequestTimeout)
  26. .set('Content-Type', 'application/json')
  27. .send(data)
  28. .then(function(res) { success(res.body) })
  29. .catch(function(err) { error(err) });
  30. }
  31. }
  32. // public rest api functions
  33. function getTodos(success, error) {
  34. _.fetch(_.todoUrl, success, error)
  35. }
  36. function getTodoById(id, success, error) {
  37. _.fetch(_.todoUrl+"/"+id, success, error)
  38. }
  39. function addTodo(todo, success, error) {
  40. _.post(_.todoUrl, todo, success, error)
  41. }
  42. }