Make WordPress Core

Opened 8 years ago

Closed 7 years ago

#39683 closed defect (bug) (fixed)

REST API JS Client: Should enable connecting to multiple endpoints at the same time

Reported by: adamsilverstein's profile adamsilverstein Owned by: adamsilverstein's profile adamsilverstein
Milestone: 4.7.5 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests commit
Focuses: javascript, rest-api Cc:

Description

Originally from @lucasstark: https://github.com/WP-API/client-js/issues/145

If you need to talk to more than one endpoint the way the api is initialized does not make this straight forward.
-
It seems like it should be possible, since wp.api.endpoints is a collection of each unique endpoint you might initialize, however, the collections and models on each endpoint will be overwritten each time you call wp.api.init, so obviously we need a solid way to initialize more than one endpoint at once.
-
An example use case would be in a multisite environment you might want to talk to two of the subsites at the same time.
-
You'd think you could do something like this rough example:

    var sites = [];
    
    wp.api.init({
        'apiRoot': 'http://example.com/subsite1/wp-json/'
    }).done(function(endpoint){
        sites[ 1 ] = endpoint;
    });

    wp.api.init({
        'apiRoot': 'http://example.com/subsite2/wp-json/'
    }).done(function(endpoint){
        sites[ 2 ] = endpoint;
    });

But now sites[1].collections is configured with the URL for the second site.

Attachments (2)

39683.diff (3.6 KB) - added by adamsilverstein 8 years ago.
39683.2.diff (5.6 KB) - added by adamsilverstein 8 years ago.

Download all attachments as: .zip

Change History (19)

#1 @adamsilverstein
8 years ago

  • Keywords has-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.8

39683.diff
Correct a name collision issue that prevented the client from properly connecting to multiple APIs as it was designed to do.

  • Don't create default model for wp.api.endpoints - this should contain only initialized endpoints
  • Add some expected defaults for versionString and apiRoot
  • Add some missing quotes around properties when searching with findWhere as per coding standards
  • Fix bug: Clean up naming conventions so inner resolvedEndpoint doesn't overwrite outer endpoint
  • Fix: deferred.resolve call - only pass the resolvedEndpoint, not the wp.api global
  • Fix: url property should be a function that dynamically returns the current model route

I'd like to add a QUnit test here to demonstrate/verify the fix by connecting to two known remote apis.

#2 follow-up: @lucasstark
8 years ago

@adamsilverstein Wanted to let you know that this seems to resolve the problems.

#3 in reply to: ↑ 2 @adamsilverstein
8 years ago

Replying to lucasstark:

@adamsilverstein Wanted to let you know that this seems to resolve the problems.

Great! Thank you for testing.

This ticket was mentioned in Slack in #core-restapi by adamsilverstein. View the logs.


8 years ago

#5 @adamsilverstein
8 years ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

#6 @adamsilverstein
8 years ago

39683.2.diff adds a unit test to verify multiple endpoints work correctly

#7 @adamsilverstein
8 years ago

  • Keywords has-unit-tests commit added; needs-unit-tests removed

#8 @adamsilverstein
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 40364:

REST API: JS Client - Enable connecting to multiple endpoints.

Enable connecting to multiple wp-api endpoints. Calling wp.api.init with a new apiRoot will parse the new endpoint's schema and store a new set of models and collections. A collection of connected endpoints is stored in wp.api.endpoints.

Props lucasstark.
Fixes #39683.

#9 @jnylen0
8 years ago

@adamsilverstein should we get this in 4.7.4 as well?

#10 @adamsilverstein
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

@jnylen0 that would make sense if there is still time. reopening for consideration, cc: @swissspidy

#11 @adamsilverstein
8 years ago

  • Milestone changed from 4.8 to 4.7.5

This ticket was mentioned in Slack in #core-restapi by adamsilverstein. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by desrosj. View the logs.


8 years ago

#14 @desrosj
8 years ago

  • Milestone changed from 4.7.5 to 4.8

#15 follow-up: @rmccue
8 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

This was only open for backporting, so closing back out now that focus has switched to 4.8.

#16 in reply to: ↑ 15 @adamsilverstein
7 years ago

  • Milestone changed from 4.8 to 4.7.5
  • Resolution fixed deleted
  • Status changed from closed to reopened

Replying to rmccue:

This was only open for backporting, so closing back out now that focus has switched to 4.8.

I don't think this ever made it into 4.7.4, can we get this merged to 4.7.5? I'd like to fix this bug for people using WordPress 4.7.

cc: @swissspidy

#17 @swissspidy
7 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 40735:

REST API: JS Client - Enable connecting to multiple endpoints.

Enable connecting to multiple wp-api endpoints. Calling wp.api.init with a new apiRoot will parse the new endpoint's schema and store a new set of models and collections. A collection of
connected endpoints is stored in wp.api.endpoints.

Props lucasstark.
Fixes #39683.

Merges [40364] to the 4.7 branch.

Note: See TracTickets for help on using tickets.