Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Help making Observable.forkJoin() work

$
0
0

@Kyrax80 wrote:

Hello guys, I’m making an interceptor to get http requests and add some headers to it.

My interceptor is as follows:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if(request.url.includes("apigee") && !request.url.includes(this.urlLoginApigee)) {   //si la peticion es hacia sanitas (que pasan por apigee) y no de login, le añadimos los headers
      request = request.clone({
        setHeaders: {
          'Content-Type':  'application/json',
          'Authorization': 'Bearer ' + this.globalVariable.apigeeToken
        }
      });
    }

    return next.handle(request)
  }

This works fine so far. Next I’ve modified the get metod from the api provider that is generated automatically in order to do the following:

  • Perform a http request (a get)
  • If the requests goes well and doesn’t return a 401 (unauthorized) error then return the response.
  • If the request gets a 401 error then call a method that gets the apigee token and then relaunch the initial request.

This is how I do it and I’m not sure if it’s the right method but works. This if from a provider that I’ll call api from now on:

get(endpoint: string, params?: any, reqOpts?: any): Observable<any> {
    if (!reqOpts) {
      reqOpts = {
        params: new HttpParams()
      };
    }

    if (params) {
      reqOpts.params = new HttpParams();
      for (let k in params) {
        reqOpts.params = reqOpts.params.set(k, params[k]);
      }
    }

    return new Observable((observer) => {
      this.http.get(endpoint, reqOpts).subscribe(response => {
          observer.next(response);
        }, (err: any) => {
            if (err.status == 401) {
              this.apigee.login().subscribe(data => {   //the login method is just a http post done to apigee to get the token
                this.apigee.setToken(data.access_token);
                this.get(endpoint, params, reqOpts).subscribe((response: any) => {
                  observer.next(response);
                });
              })
            }
        });
    }).catch(error => {return Observable.throw("");});
  }

Ok now the problem is that if I try to do

Observable.forkJoin(method1, method2).subscribe(data => console.log("hello"))

Where method1 and method2 are two method that return an Observable using the get method that I put above.

With this, the forkJoin doesn’t get inside, it never prints hello

And I’ve proven that it’s because of how I have the get method from the api provider because if I use the http angular library directly it works perfectly.

Or even if I use the following:

Observable.forkJoin(Observable.of(1), Observable.of(2)).subscribe(data => console.log("hello"))

It works.

Does anyone know how to fix my get method to make this work?

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>