Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client and services #771

Merged
merged 22 commits into from
May 14, 2020
Merged

Client and services #771

merged 22 commits into from
May 14, 2020

Conversation

ob-stripe
Copy link
Contributor

@ob-stripe ob-stripe commented Oct 18, 2019

r? @ob-stripe @remi-stripe
cc? @stripe/api-libraries

Introduces a "client + services" API.

This means, for example, instead of

\Stripe\Stripe::setApiKey('sk_test_xxx');
$customer = \Stripe\Customer::retrieve(
  'cus_HG97q60gg1Naih'
);
$customer->delete();

where retrieve, create, update and list are static "collection" methods that live on the class, and save and delete are instance methods

users do

$stripe = new \Stripe\StripeClient([
  'api_key' => 'sk_test_xxx',
]);
$stripe->customers->delete('cus_HG97q60gg1Naih', []);

where all methods are instance methods of "service" objects that can be accessed via getters from an initialized $stripe client.

This has some advantages. It permits invoking "delete" without a previous "retrieve". It permits multiple StripeClients to coexist, with different settings. It also is generally more consistent and more amenable to codegen.

How does $stripe->customers->delete work, behind the scenes?

  1. $stripe->customers triggers StripeClient.__get
  2. StripeClient.__get lazily initializes new CoreServiceFactory and calls CoreServiceFactory.__get, which is implemented in AbstractServiceFactory.__get
  3. AbstractServiceFactory.__get calls CoreServiceFactory.getServiceClass
  4. CoreServiceFactory.getServiceClass references a static $classMap, which has been codegenned mapping from names like 'customers' to ServiceClasses, like CustomerService, and lazily initializes the corresponding 'Service' class..
  5. That class has methods like ->delete, which have also been codegenned, and use AbstractService.request, and AbstractService.buildPath.
  6. AbstractService.request ends up calling StripeClient.request (which is implemented in BaseStripeClient.request) to actually perform the HTTP request, as the StripeClient has been passed through through every stage of lazy initialization.

For namespaced resources, CoreServiceFactory.getServiceClass actually does not return a Service class directly, it returns another Service factory, and then that service factory has a .__get and .getServiceClass that resolve to services.

@ob-stripe ob-stripe force-pushed the integration-services branch 2 times, most recently from 0a746b7 to baaca7f Compare October 31, 2019 17:34
@ob-stripe ob-stripe force-pushed the integration-services branch from baaca7f to 8877947 Compare January 10, 2020 20:44
@ob-stripe ob-stripe force-pushed the integration-services branch 2 times, most recently from 3988b57 to e736b9d Compare February 1, 2020 04:50
@ob-stripe ob-stripe force-pushed the integration-services branch 2 times, most recently from 1a64507 to 25e23c1 Compare February 13, 2020 02:10
@ob-stripe ob-stripe force-pushed the integration-services branch 4 times, most recently from 3af96c2 to 8ca640d Compare February 27, 2020 07:16
@ob-stripe ob-stripe force-pushed the integration-services branch 3 times, most recently from bb26e09 to 9e1c9a0 Compare March 14, 2020 01:26
@ob-stripe ob-stripe force-pushed the integration-services branch 2 times, most recently from 69185d0 to 66b0dcd Compare March 24, 2020 21:03
@ob-stripe ob-stripe force-pushed the integration-services branch from 95a1cf8 to 4768ab4 Compare April 3, 2020 22:30
@ob-stripe ob-stripe force-pushed the integration-services branch from 1a024e2 to 8a482aa Compare May 1, 2020 01:51
@richardm-stripe richardm-stripe changed the title [WIP] Client and services Client and services May 11, 2020
Copy link
Contributor Author

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I can't approve my own PR :)

README.md Outdated Show resolved Hide resolved
@richardm-stripe richardm-stripe merged commit 962f1ca into master May 14, 2020
@richardm-stripe richardm-stripe deleted the integration-services branch May 14, 2020 15:15
@driesvints
Copy link

I don't immediately see it mentioned here but since this was released in a minor release I suspect the old way of doing things is still accounted for?

@remi-stripe
Copy link
Contributor

@driesvints That's correct, we're not removing the old way, we're just adding this new approach and will recommend it moving forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants