HTTP.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 getBlob(token) {
  11. return await axios({
  12. url: this.url,
  13. method: 'get',
  14. responseType: 'blob',
  15. headers: {
  16. 'Authorization': `Bearer ${token}`
  17. }
  18. })
  19. }
  20. async getQuery(token) {
  21. return await axios.get(this.url, {
  22. headers: {
  23. 'Authorization': `Bearer ${token}`
  24. }
  25. })
  26. }
  27. async getXHR(token) {
  28. if (!token) {
  29. let response = await axios({
  30. method: "GET",
  31. url: this.url,
  32. headers: {
  33. 'Content-Type': 'application/json',
  34. },
  35. })
  36. return await response.data;
  37. }
  38. const MyHeaders = {
  39. 'Authorization': 'Bearer ' + token,
  40. 'Content-Type': 'application/json',
  41. }
  42. let response = await axios({
  43. method: "GET",
  44. url: this.url,
  45. headers: MyHeaders,
  46. })
  47. // console.log('response', response)
  48. return await response.data;
  49. }
  50. async get(token) {
  51. return axios.get(this.url, {
  52. headers: {
  53. 'Authorization': `Bearer ${token}`
  54. }
  55. })
  56. .then((res) => {
  57. return res;
  58. })
  59. .catch((error) => {
  60. console.log("ERROR :: ", error)
  61. return new Error("GG");
  62. })
  63. }
  64. async post(body, token) {
  65. if (!token) {
  66. let response = await axios({
  67. method: "POST",
  68. url: this.url,
  69. headers: {
  70. 'Content-Type': 'application/json',
  71. },
  72. data: body
  73. })
  74. return await response.data;
  75. }
  76. const MyHeaders = {
  77. 'Authorization': 'Bearer ' + token,
  78. }
  79. let response = await axios({
  80. method: "POST",
  81. url: this.url,
  82. headers: MyHeaders,
  83. data: body
  84. })
  85. console.log('response', response)
  86. return await response.data;
  87. }
  88. async postQuery(body, token) {
  89. if (!token) {
  90. return await axios({
  91. method: "POST",
  92. url: this.url,
  93. headers: {
  94. 'Content-Type': 'application/json',
  95. },
  96. data: body
  97. })
  98. }
  99. const MyHeaders = {
  100. 'Authorization': 'Bearer ' + token,
  101. }
  102. return await axios({
  103. method: "POST",
  104. url: this.url,
  105. headers: MyHeaders,
  106. data: body
  107. })
  108. }
  109. async put(body, token) {
  110. if (!token) {
  111. let response = await axios({
  112. method: "PUT",
  113. url: this.url,
  114. headers: {
  115. 'Content-Type': 'application/json',
  116. },
  117. data: body
  118. })
  119. return await response.data;
  120. }
  121. const MyHeaders = {
  122. 'Authorization': 'Bearer ' + token,
  123. }
  124. let response = await axios({
  125. method: "PUT",
  126. url: this.url,
  127. headers: MyHeaders,
  128. data: body
  129. })
  130. console.log('response', response)
  131. return await response.data;
  132. }
  133. async putQuery(body, token) {
  134. if (!token) {
  135. return await axios({
  136. method: "PUT",
  137. url: this.url,
  138. headers: {
  139. 'Content-Type': 'application/json',
  140. },
  141. data: body
  142. })
  143. }
  144. const MyHeaders = {
  145. 'Authorization': 'Bearer ' + token,
  146. }
  147. return await axios({
  148. method: "PUT",
  149. url: this.url,
  150. headers: MyHeaders,
  151. data: body
  152. })
  153. }
  154. }