Advertisement
kopyl

Untitled

Mar 24th, 2022
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from "@angular/core"
  2. import { Observable } from "rxjs"
  3. import { HttpClient, HttpParams } from "@angular/common/http"
  4. import { HttpErrorHandlerService } from "@services/http-error-handler"
  5. import { makeUrl } from "@utils/constructors"
  6.  
  7. const port = 5001
  8. const protocol = "http"
  9.  
  10. const URLS = {
  11.     AUTH: makeUrl("authorize", port, protocol),
  12.     NOTES: {
  13.         GET: makeUrl("notes", port, protocol),
  14.     },
  15.     ONLINE: makeUrl("users/online", port, protocol),
  16. }
  17.  
  18. abstract class Request {
  19.     protected success: Function
  20.     protected request: Observable<any>
  21.     protected params = new HttpParams()
  22.     protected errorMessage: string
  23.     protected body: Object = {}
  24.     protected abstract method: string
  25.     protected abstract URL: string
  26.  
  27.     constructor(
  28.         public http: HttpClient,
  29.         public HTTPErrorHandler: HttpErrorHandlerService
  30.     ) {}
  31.  
  32.     protected makeParams(kwargs: Object) {}
  33.  
  34.     protected makeRequest() {
  35.         switch (this.method) {
  36.             case "GET":
  37.                 this.request = this.http.get(this.URL, {
  38.                     params: this.params,
  39.                 })
  40.                 break
  41.             case "POST":
  42.                 this.request = this.http.post(this.URL, this.body, {
  43.                     params: this.params,
  44.                 })
  45.                 break
  46.             case "PUT":
  47.                 this.request = this.http.put(this.URL, this.body, {
  48.                     params: this.params,
  49.                 })
  50.                 break
  51.         }
  52.     }
  53.  
  54.     public set onSuccess(func: Function) {
  55.         this.success = func
  56.     }
  57.  
  58.     public send(kwargs: any = null): void {
  59.         this.makeParams(kwargs)
  60.         this.makeRequest()
  61.  
  62.         const actions = {
  63.             error: (error) =>
  64.                 this.HTTPErrorHandler.handle(error, this.errorMessage),
  65.         }
  66.  
  67.         if (this.success) {
  68.             actions["next"] = this.success
  69.         }
  70.  
  71.         this.request.subscribe(actions)
  72.     }
  73. }
  74.  
  75. class Auth extends Request {
  76.     method = "GET"
  77.     URL = URLS.AUTH
  78.  
  79.     override makeParams(kwargs: authSendArgs) {
  80.         const googleAuthData = JSON.stringify(kwargs.oauthData)
  81.         this.params = new HttpParams().set("google_auth_data", googleAuthData)
  82.     }
  83. }
  84.  
  85. class Notes {
  86.     get: GetNotes
  87.  
  88.     constructor(
  89.         public http: HttpClient,
  90.         public HTTPErrorHandler: HttpErrorHandlerService
  91.     ) {
  92.         this.get = new GetNotes(http, HTTPErrorHandler)
  93.     }
  94. }
  95.  
  96. class GetNotes extends Request {
  97.     method = "GET"
  98.     URL = URLS.NOTES.GET
  99.     override errorMessage = "notes"
  100. }
  101.  
  102. class Online extends Request {
  103.     method = "PUT"
  104.     URL = URLS.ONLINE
  105.     override errorMessage = "online"
  106. }
  107.  
  108. @Injectable({
  109.     providedIn: "root",
  110. })
  111. export class RequestsService {
  112.     auth: Auth
  113.     notes: Notes
  114.     online: Online
  115.  
  116.     constructor(
  117.         private http: HttpClient,
  118.         private HTTPErrorHandler: HttpErrorHandlerService
  119.     ) {
  120.         this.auth = new Auth(http, HTTPErrorHandler)
  121.         this.notes = new Notes(http, HTTPErrorHandler)
  122.         this.online = new Online(http, HTTPErrorHandler)
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement