|
1 | 1 | import { Injectable } from '@angular/core';
|
2 |
| -import { Http } from '@angular/http'; |
| 2 | +import { Http, ResponseContentType } from '@angular/http'; |
3 | 3 | import { Observable } from 'rxjs/Observable';
|
4 | 4 | // Operators
|
5 | 5 | import 'rxjs/add/operator/map';
|
6 | 6 | import 'rxjs/add/operator/catch';
|
7 | 7 |
|
| 8 | +import { environment } from 'environments/environment'; |
| 9 | +import { Microservice, MicroserviceList } from 'app/shared/proto/microservices_pb'; |
| 10 | + |
8 | 11 |
|
9 | 12 | @Injectable()
|
10 | 13 | export class NavTreeDataService {
|
11 | 14 |
|
12 | 15 | constructor(private http: Http) { }
|
13 | 16 |
|
| 17 | + public getProto(): Observable<any> { |
| 18 | + return this.http.get(environment.microservicesEndpoint, { responseType: ResponseContentType.ArrayBuffer }) |
| 19 | + .map((data: any) => { |
| 20 | + /** |
| 21 | + * The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. |
| 22 | + * You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the |
| 23 | + * typed array objects or a DataView object which represents the buffer in a specific format, |
| 24 | + * and use that to read and write the contents of the buffer. |
| 25 | + * |
| 26 | + * google-protobuf/protobuf.js requires a byte-level view on the data, which is provided by Uint8Array |
| 27 | + * in the browser (falls back to Array in older browsers) or Buffer under node.js. |
| 28 | + * You are actually not converting the ArrayBuffer, just wrapping it in a byte-level view. |
| 29 | + * Basically, you tell JS how to interpret the raw binary data. |
| 30 | + * |
| 31 | + * This is done because decoders work pretty much the same in all environments with a few exceptions, |
| 32 | + * and an additional check-and-wrap for ArrayBuffer (as either Buffer or Uint8Array), while certainly possible, |
| 33 | + * would usually be unnecessary overhead under node.js / in older browsers / where Uint8Array is used anyway. |
| 34 | + */ |
| 35 | + const test = data; |
| 36 | + const serializedData = new Uint8Array(data.arrayBuffer()); |
| 37 | + const microserviceList = MicroserviceList.deserializeBinary(serializedData); |
| 38 | + return microserviceList.toObject(); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
14 | 42 | public getJSON(): Observable<any> {
|
15 | 43 | return this.http.get('assets/nav-tree-data.json')
|
16 |
| - .map((data: any) => data.json()) |
17 |
| - .catch(error => error); |
| 44 | + .map((data: any) => data.json()); |
18 | 45 | }
|
19 | 46 |
|
20 | 47 | }
|
0 commit comments