BaseApi.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { OpenApiError } from '../api/OpenApiError';
  2. export class BaseApi {
  3. /** Die URL des Servers. Alle Pfadangaben sind relativ zu dieser URL. */
  4. protected url : string;
  5. /** Der Anmeldename beim Server */
  6. protected username : string;
  7. /** Der Default-RequestInit für einen Fetch */
  8. #requestinit : RequestInit = {
  9. cache : 'no-cache',
  10. credentials : 'same-origin'
  11. }
  12. /** Die Default-Header-Einträge */
  13. #headers : Record<string, string> = {};
  14. /**
  15. * Erstellt eine neue API mit der übergebenen Konfiguration.
  16. *
  17. * @param {string} url - die URL des Servers: Alle Pfadangaben sind relativ zu dieser URL
  18. * @param {string} username - der Benutzername für den API-Zugriff
  19. * @param {string} password - das Kennwort des Benutzers für den API-Zugriff
  20. */
  21. protected constructor(url : string, username : string, password : string) {
  22. this.url = url;
  23. this.username = username;
  24. this.#headers["Authorization"] = "Basic " + btoa(username + ":" + password);
  25. }
  26. #getURL(path : string) : string {
  27. return this.url + path;
  28. }
  29. async #getBinary(path : string, mimetype : string) : Promise<Blob> {
  30. let requestInit : RequestInit = { ...this.#requestinit };
  31. requestInit.headers = { ...this.#headers };
  32. requestInit.headers["Accept"] = mimetype;
  33. requestInit.body = null;
  34. requestInit.method = 'GET';
  35. try {
  36. const response = await fetch(this.#getURL(path), requestInit);
  37. if (!response.ok)
  38. throw new OpenApiError(response, 'Fetch failed for GET: ' + path);
  39. return await response.blob();
  40. } catch (e) {
  41. if (e instanceof Error)
  42. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for GET: ' + path);
  43. throw new Error("Unexpected Error: " + e);
  44. }
  45. }
  46. public getPDF(path : string) : Promise<Blob> {
  47. return this.#getBinary(path, 'application/pdf');
  48. }
  49. async #getTextBased(path : string, mimetype : string) : Promise<string> {
  50. let requestInit : RequestInit = { ...this.#requestinit };
  51. requestInit.headers = { ...this.#headers };
  52. requestInit.headers["Accept"] = mimetype;
  53. requestInit.body = null;
  54. requestInit.method = 'GET';
  55. try {
  56. const response = await fetch(this.#getURL(path), requestInit);
  57. if (!response.ok)
  58. throw new OpenApiError(response, 'Fetch failed for GET: ' + path);
  59. return await response.text();
  60. } catch (e) {
  61. if (e instanceof Error)
  62. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for GET: ' + path);
  63. throw new Error("Unexpected Error: " + e);
  64. }
  65. }
  66. public getText(path : string) : Promise<string> {
  67. return this.#getTextBased(path, 'text/plain');
  68. }
  69. public getJSON(path : string) : Promise<string> {
  70. return this.#getTextBased(path, 'application/json');
  71. }
  72. async #postTextBased(path : string, mimetype_send : string, mimetype_receive : string, body : string | null) : Promise<string> {
  73. let requestInit : RequestInit = { ...this.#requestinit };
  74. requestInit.headers = { ...this.#headers };
  75. requestInit.headers["Content-Type"] = mimetype_send;
  76. requestInit.headers["Accept"] = mimetype_receive;
  77. requestInit.body = body;
  78. requestInit.method = 'POST';
  79. try {
  80. const response = await fetch(this.#getURL(path), requestInit);
  81. if (!response.ok)
  82. throw new OpenApiError(response, 'Fetch failed for POST: ' + path);
  83. return await response.text();
  84. } catch (e) {
  85. if (e instanceof Error)
  86. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for POST: ' + path);
  87. throw new Error("Unexpected Error: " + e);
  88. }
  89. }
  90. public postText(path : string, body : string | null) : Promise<string> {
  91. return this.#postTextBased(path, 'text/plain', 'text/plain', body);
  92. }
  93. public postJSON(path : string, body : string | null) : Promise<string> {
  94. return this.#postTextBased(path, 'application/json', 'application/json', body);
  95. }
  96. async #patchTextBased(path : string, mimetype : string, body : string) : Promise<void> {
  97. let requestInit : RequestInit = { ...this.#requestinit };
  98. requestInit.headers = { ...this.#headers };
  99. requestInit.headers["Content-Type"] = mimetype;
  100. requestInit.body = body;
  101. requestInit.method = 'PATCH';
  102. try {
  103. const response = await fetch(this.#getURL(path), requestInit);
  104. if (!response.ok)
  105. throw new OpenApiError(response, 'Fetch failed for PATCH: ' + path);
  106. return;
  107. } catch (e) {
  108. if (e instanceof Error)
  109. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for PATCH: ' + path);
  110. throw new Error("Unexpected Error: " + e);
  111. }
  112. }
  113. public patchText(path : string, body : string) : Promise<void> {
  114. return this.#patchTextBased(path, 'text/plain', body);
  115. }
  116. public patchJSON(path : string, body : string) : Promise<void> {
  117. return this.#patchTextBased(path, 'application/json', body);
  118. }
  119. async #putTextBased(path : string, mimetype : string, body : string) : Promise<void> {
  120. let requestInit : RequestInit = { ...this.#requestinit };
  121. requestInit.headers = { ...this.#headers };
  122. requestInit.headers["Content-Type"] = mimetype;
  123. requestInit.body = body;
  124. requestInit.method = 'PUT';
  125. try {
  126. const response = await fetch(this.#getURL(path), requestInit);
  127. if (!response.ok)
  128. throw new OpenApiError(response, 'Fetch failed for PUT: ' + path);
  129. return;
  130. } catch (e) {
  131. if (e instanceof Error)
  132. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for PUT: ' + path);
  133. throw new Error("Unexpected Error: " + e);
  134. }
  135. }
  136. public putText(path : string, body : string) : Promise<void> {
  137. return this.#putTextBased(path, 'text/plain', body);
  138. }
  139. public putJSON(path : string, body : string) : Promise<void> {
  140. return this.#putTextBased(path, 'application/json', body);
  141. }
  142. async #deleteTextBased(path : string, mimetype : string) : Promise<void> {
  143. let requestInit : RequestInit = { ...this.#requestinit };
  144. requestInit.headers = { ...this.#headers };
  145. requestInit.headers["Accept"] = mimetype;
  146. requestInit.body = null;
  147. requestInit.method = 'DELETE';
  148. try {
  149. const response = await fetch(this.#getURL(path), requestInit);
  150. if (!response.ok)
  151. throw new OpenApiError(response, 'Fetch failed for DELETE: ' + path);
  152. return;
  153. } catch (e) {
  154. if (e instanceof Error)
  155. throw (e instanceof OpenApiError) ? e : new OpenApiError(e, 'Fetch failed for DELETE: ' + path);
  156. throw new Error("Unexpected Error: " + e);
  157. }
  158. }
  159. public deleteText(path : string) : Promise<void> {
  160. return this.#deleteTextBased(path, 'text/plain');
  161. }
  162. public deleteJSON(path : string) : Promise<void> {
  163. return this.#deleteTextBased(path, 'application/json');
  164. }
  165. }