Reac front end for psicometric app

HTTP.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import axios from 'axios';
  2. export class Service {
  3. constructor(path) {
  4. this.base_url = 'http://204.48.25.93:8081'
  5. this.url = this.base_url + path
  6. // this.base_url = 'http://psicoadmin.ditca.org:8081'
  7. // this.api = 'http://204.48.25.93:8081/user?user=patrik&password=12345'
  8. // this.api = 'http://psicoadmin.ditca.org:8081/user?user=patrik&password=12345'
  9. }
  10. async getQuery(token) {
  11. return await axios.get(this.url, {
  12. headers: {
  13. 'Authorization': `Bearer ${token}`
  14. }
  15. })
  16. }
  17. async get(token) {
  18. return axios.get(this.url, {
  19. headers: {
  20. 'Authorization': `Bearer ${token}`
  21. }
  22. })
  23. .then((res) => {
  24. return res;
  25. })
  26. .catch((error) => {
  27. console.log("ERROR :: ", error)
  28. return new Error("GG");
  29. })
  30. }
  31. async post(body, token) {
  32. if (!token) {
  33. let response = await axios({
  34. method: "POST",
  35. url: this.url,
  36. headers: {
  37. 'Content-Type': 'application/json',
  38. },
  39. data: body
  40. })
  41. return await response.data;
  42. }
  43. const MyHeaders = {
  44. 'Authorization': 'Bearer ' + token,
  45. }
  46. let response = await axios({
  47. method: "POST",
  48. url: this.url,
  49. headers: MyHeaders,
  50. data: body
  51. })
  52. console.log('response', response)
  53. return await response.data;
  54. }
  55. async postQuery(body, token) {
  56. if (!token) {
  57. return await axios({
  58. method: "POST",
  59. url: this.url,
  60. headers: {
  61. 'Content-Type': 'application/json',
  62. },
  63. data: body
  64. })
  65. }
  66. const MyHeaders = {
  67. 'Authorization': 'Bearer ' + token,
  68. }
  69. return await axios({
  70. method: "POST",
  71. url: this.url,
  72. headers: MyHeaders,
  73. data: body
  74. })
  75. }
  76. async put(body, token) {
  77. if (!token) {
  78. let response = await axios({
  79. method: "PUT",
  80. url: this.url,
  81. headers: {
  82. 'Content-Type': 'application/json',
  83. },
  84. data: body
  85. })
  86. return await response.data;
  87. }
  88. const MyHeaders = {
  89. 'Authorization': 'Bearer ' + token,
  90. }
  91. let response = await axios({
  92. method: "PUT",
  93. url: this.url,
  94. headers: MyHeaders,
  95. data: body
  96. })
  97. console.log('response', response)
  98. return await response.data;
  99. }
  100. async putQuery(body, token) {
  101. if (!token) {
  102. return await axios({
  103. method: "PUT",
  104. url: this.url,
  105. headers: {
  106. 'Content-Type': 'application/json',
  107. },
  108. data: body
  109. })
  110. }
  111. const MyHeaders = {
  112. 'Authorization': 'Bearer ' + token,
  113. }
  114. return await axios({
  115. method: "PUT",
  116. url: this.url,
  117. headers: MyHeaders,
  118. data: body
  119. })
  120. }
  121. }