0

I am trying to get the result of one method that returns an observable to pass into the second request

this.runfirstmethod(headers).mergeMap(response => this.http[verb](url, response).map(res => res.json()))

This is everything i am doing, I think i am doing something wrong because i cant get the headers from buildAzureHeaders.

Service

    buildAzureHeaders(headers): Observable<Headers> {

    if (headers == null) {
        headers = new Headers();
    }

    if (JSON.parse(localStorage.getItem('azureToken')).token == null || this.azureAccessToken == null) {
        this.Adal4Service.acquireToken("https://graph.windows.net").map(token => {
            this.azureAccessToken = token;
            localStorage.setItem('azureToken', token);
            headers.append('Authorization', "Bearer " + token);
            return Observable.of(headers);
        });
    } else {
        headers.append('Authorization', "Bearer " + localStorage.setItem('azureToken', JSON.parse(localStorage.getItem('azureToken')).token));
        console.log("Headers: " + headers);
        return Observable.of(headers);
    }
}

invokeRequest(verb, url, headers?, body?): Observable<any> {
    return this.buildAzureHeaders(headers).switchMap(builtHeaders => this.http[verb](url, builtHeaders))
}

component

this.ApiAuthService.invokeRequest("get","https://graph.windows.net/aseracare.com/users?api-version=1.6").subscribe(res => {
    console.log(res);
  });
user1552172
  • 614
  • 1
  • 9
  • 27

1 Answers1

2
class MyClass {

    firstMethod(headers: object): Observable<object> {
        return Observable.of({qwerty: 123456});
    }

    secondMethod(something: object): Observable<string> {
        return Observable.of('OK');
    }

    runner(): void {
        const obj = {a:1};
        this
            .firstMethod(obj)   // object -> Observable
            .switchMap(         // Observable -> object
                (returnedObject: object) => this.secondMethod(returnedObject)  // object -> Observable
            ).subscribe((x: string) => console.log(x)) // logs 'OK'
    }
}

SwitchMap "converts" first observable into a value that can be received by the second one.

For more precise documentation check here and here

Marian
  • 3,789
  • 2
  • 26
  • 36
  • I am getting an undefined from the first method which works if i subscribe to it normally. return Observable.of(headers); this should make the switchmap happen but i get Cannot read property 'switchMap' of undefined – user1552172 Sep 27 '17 at 19:46
  • @user1552172 `runfirstmethod` what is this method's signature? – Marian Sep 28 '17 at 02:03
  • I updated the entire service and component, i cannot get the map to fire to return into the switchmap. – user1552172 Sep 28 '17 at 19:10
  • @user1552172 `this.Adal4Service.acquireToken` - you need to return it. At the moment, the first `if` branch does not return any value. – Marian Sep 28 '17 at 19:50
  • return Observable.of(headers); does that not return, darn i was hoping i could use this seperate method to build the headers with access tokens etc. – user1552172 Sep 29 '17 at 14:03
  • @user1552172 Your current process is kinda: 1. If no headers, create new headers. 2.1 If no token, acquire token (without returning it). 2.2 Otherwise, return headers with existing token. – Marian Sep 29 '17 at 14:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155610/discussion-between-user1552172-and-hlfrmn). – user1552172 Sep 29 '17 at 15:25