0

To implement the Yelp API a signature is needed. Therefor, this signature should be generated while passing all the parameters for the URL which gonna be use.

Long story short, since I query the API within the help of jsonp, the tricks was to generate first the next JSONP callback ID, to include it in the signature generation (reference: how to custom set angularjs jsonp callback name?)

In Angular 1 something like:

// Prepare parameters
var callbackId = (<any> angular).callbacks.counter.toString(36);
// Update my params with: callback='angular.callbacks._' + callbackId
// Query my backend to generate a signature
// Query Yelp API

Now that I'm migrating to angular2, I've to migrate that voodoo magic too. Anyone have got an idea how could I query the next jsonp callback ID which gonna be use respectively how to replace

(<any> angular).callbacks.counter

in angular2?

I saw that BrowserJsonp contains a nextRequestID() but I didn't found a way to access it (it's a private factory if I'm not wrong).

P.S.: For the record, I found that

angular.callbacks._ID

should be replaced in my angular2 with

__ng_jsonp__.__reqID.finished
Community
  • 1
  • 1
David Dal Busco
  • 7,975
  • 15
  • 55
  • 96

1 Answers1

0

Digging a little bit in that problem, I didn't found any other solution than writing my own @Injectable service where I perform all my jsonp queries of my app and incrementing my own counter for counting the jsonp callback (class variable, having only one instance allow me to have that counter).

Pseudo-code:

@Injectable()
export class JsonpService {
    private counter:number=0;

    private query1() {
         this.jsonp.get(url, options).then(() => {
             counter++;
         }, () => {
             counter++;
         });
    }

    private getActualJsonpCallbackCounter() {
        return counter; 
    }
 }

Still, I found that solution quite ugly, for that reason I migrated my application from using the V2 to the V3 API of Yelp. Yelp API V3 implement OAuth2 (and more more OAuth1) which doesn't request the encryption of the all URL aka which doesn't need that ugly tricks.

Note: While migrating from V2 to V3, I had to migrate too from using a "jsonp" request to a "get" request (the OAuth2 authorization have to be added in the header of the request). Therefor I faced some CORS problem which where solved and documented in following post

Angular2 Yelp API CORS error

Community
  • 1
  • 1
David Dal Busco
  • 7,975
  • 15
  • 55
  • 96