2016-12-11 68 views
1

更新找到解決方案如何使一個鏈接請求(各種數字)與角2

我想從Instagram的API(https://api.instagram.com/v1/tags/ {標籤名稱} /媒體/最近的所有網頁?=的access_token接入 - TOKEN) 在它的響應中有'next_url'參數,使用它可以獲得新頁面。 我嘗試過遞歸,但它在~13步驟中查殺瀏覽器; 組件:

import { Component, OnInit } from '@angular/core'; 
import {InstagramService} from './instagram.service'; 

import {Search} from '../search'; 

@Component({ 
    selector: 'app-test', 
    templateUrl: './test.component.html', 
    styleUrls: ['./test.component.css'] 
}) 
export class TestComponent implements OnInit { 

    posts: Post[]; 
    tag: String = 'blabla23'; 
    constructor(
    private instagramService: InstagramService 
) {} 

    ngOnInit() { 
    this.getPosts(); 
    this.posts = []; 
    } 

    getPosts(nextUrl?: string): void { 
    this.instagramService 
     .getTagMedia(this.tag, nextUrl) 
     .then((result: Search) => { 
      this.posts = this.posts.concat(this.posts, result.getMedia()); 
      if (result.getNextUrl() != null) { 
       this.getPosts(result.getNextUrl().toString()); 
      } 
     }); 
    } 

} 

服務:

import { Injectable } from '@angular/core'; 
import { Jsonp, Response, Headers, RequestOptions} from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/toPromise'; 
import 'rxjs/add/operator/delay'; 
import { Search } from './search'; 

@Injectable() 
export class InstagramService { 
    private apiUrl = 'https://api.instagram.com/v1/'; 
    private token = 'blalba'; 

    constructor (private jsonp: Jsonp) {} 

    public getTagMedia(tag: String, url: string|null): Promise<Search> { 
    url = url || `${this.apiUrl}tags/${tag}/media/recent?access_token=${this.token}`; 
    url = `${url}&callback=JSONP_CALLBACK` 
    return this.jsonp.get(url) 
        .toPromise() 
        .then(response => { return new Search(response.json());}) 
    } 
} 

搜索模式:

export class Search { 
    private data; 
    private metadata; 
    private pagination; 

    constructor(row) { 
    this.data = row.data; 
    this.metadata = row.metadata; 
    this.pagination = row.pagination; 
    } 

    public getNextUrl(): string | null { 
    return this.pagination.next_url || null; 
    } 

    public getMedia() { 
    return this.data; 
    } 

}

我知道Observable.flatMap(),但未能在這種情況下使用它由於未知數量的請求。

如何獲取所有的api頁面? ps我是Angular 2和js的新手。 pps對不起我的英語

回答

0

我做到了(遞歸和承諾)錯誤。 工作解決方案(服務):

@Injectable() 
export class InstagramService { 
    private apiUrl = 'https://api.instagram.com/v1/'; 
    private token = 'blabla'; 
    private pagesLimit = 5; 

    constructor (private jsonp: Jsonp) {} 

    public getTagMedia(tag: string): Promise<Post[]> { 
    let url = `${this.apiUrl}tags/${tag}/media/recent?access_token=${this.token}`; 
    url = `${url}&callback=JSONP_CALLBACK`; 

    return this.getPosts(url, 0); 
    } 

    public getPosts(url: string, page: number): Promise<Post[]> { 

    return this.jsonp.get(url) 
     .delay(200) 
     .toPromise() 
     .then(response => { 
      let search = new Search(response.json()); 
      let nextUrl = search.getNextUrl(); 

      if (nextUrl && page < this.pagesLimit) { 
       nextUrl = `${nextUrl}&callback=JSONP_CALLBACK`; 
       return this.getPosts(nextUrl, ++page) 
          .then(res => search.getMedia().concat(res)) 
      } else { 
       return search.getMedia(); 
      } 
     }) 
     .catch(this.handleError); 
    } 

    private handleError (error: Response | any) { 
    console.log(error); 
    } 
}