0

My API has a get method that exposes 4 objects that contain 1 href in each, i need to make 4 get requests with the href's after doing the first GET

API contract

{
  "tipoDocumentoCliente": "CPF",
  "documentoCliente": "9687023767",
  "nomeCliente": "RAFAEL HERMOGENES DE MENDONCA",
  "dataNascimentoCliente": "1982-04-14T00:00:00",
  "sexoCliente": "MASCULINO",
  "estadoCivilCliente": "CASADO",
  "cpfCliente": "9687023767",
  "cnpjCliente": null,
  "celCliente": null,
  "profissaoCliente": "Analista de desenvolvimento de sistemas",
  "rendaCliente": 12000,
  "escolaridadeCliente": null,
  "matricula": 0,
  "contas": {
    "method": "Get",
    "rel": "get-contas",
    "href": "http://localhost:62474/api/v1/Contas/9687023767"
  },
  "enderecos": {
    "method": "Get",
    "rel": "get-enderecos",
    "href": "http://localhost:62474/api/v1/Endereco/9687023767"
  },
  "telefones": {
    "method": "Get",
    "rel": "get-telefones",
    "href": "http://localhost:62474/api/v1/Phones/9687023767"
  },
  "emails": {
    "method": "Get",
    "rel": "get-emails",
    "href": "http://localhost:62474/api/v1/Emails/9687023767"
  }
}

This i how i do a single request to my api

public ObterFundos<T>(cpf: number): Observable<T> {
    return this.http.get<T>(this._calcularFundosUrl + cpf);
}

i have no idea how im going to use the hrefs inside the request

1 Answer 1

1

You need to subscribe your method first in component then use forkJoin to get and combine data from different URL

component.ts

desiredData: any;

this.service.ObterFundos(<number>).subscribe(res => {
    forkJoin(this.service.getDataFromUrl(res.contas.href), 
                this.service.getDataFromUrl(res.enderecos.href), 
                this.service.getDataFromUrl(res.telefones.href), 
                this.service.getDataFromUrl(res.emails.href)).subscribe(result => {
        res.contas['data'] = result[0];
        res.enderecos['data'] = result[1];
        res.telefones['data'] = result[2];
        res.emails['data'] = result[3];
        this.desiredData = res;
        console.log(this.desiredData);
    });
});

import {forkJoin} from 'rxjs/observable/forkJoin';

service.ts

public ObterFundos<T>(cpf: number): Observable<T> {
    return this.http.get<T>(this._calcularFundosUrl + cpf);
}

getDataFromUrl(url) {
    return this.http.get(url);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude, i was already doing something similar, but your implementation is much cleaner than mine! Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.