22
33/*
44 * Encore Digital Group - Planning Center PHP SDK
5- * Copyright (c) 2023-2024 . Encore Digital Group
5+ * Copyright (c) 2023-2025 . Encore Digital Group
66 */
77
88namespace EncoreDigitalGroup \PlanningCenter \Traits ;
99
1010use EncoreDigitalGroup \PlanningCenter \Objects \SdkObjects \ClientResponse ;
11+ use EncoreDigitalGroup \PlanningCenter \Support \AuthType ;
1112use EncoreDigitalGroup \PlanningCenter \Support \PlanningCenterApiVersion ;
13+ use EncoreDigitalGroup \StdLib \Objects \Support \Types \Str ;
1214use Illuminate \Http \Client \PendingRequest ;
1315use Illuminate \Http \Client \Response ;
1416use PHPGenesis \Http \HttpClient ;
@@ -18,22 +20,33 @@ trait HasPlanningCenterClient
1820{
1921 protected const string HOSTNAME = "https://api.planningcenteronline.com " ;
2022
23+ /** @experimental This could change. Use with caution. */
24+ protected const string X_PCO_API_VERSION_HEADER = "X-PCO-API-Version " ;
25+
2126 protected string $ clientId ;
2227 protected string $ clientSecret ;
2328 protected string $ apiVersion = "" ;
2429
30+ /** @experimental This could change. Use with caution. */
31+ protected AuthType $ authType = AuthType::Basic;
32+
2533 public function __construct (?string $ clientId = null , ?string $ clientSecret = null )
2634 {
27- $ this ->clientId = $ clientId ?? "" ;
28- $ this ->clientSecret = $ clientSecret ?? "" ;
35+ $ this ->clientId = $ clientId ?? Str:: empty () ;
36+ $ this ->clientSecret = $ clientSecret ?? Str:: empty () ;
2937
3038 new HttpClientBuilder ;
3139 }
3240
3341 public function client (): PendingRequest
3442 {
43+ if ($ this ->authType == AuthType::Token) {
44+ return HttpClient::withToken ($ this ->clientId )
45+ ->withHeader (self ::X_PCO_API_VERSION_HEADER , $ this ->apiVersion );
46+ }
47+
3548 return HttpClient::withBasicAuth ($ this ->clientId , $ this ->clientSecret )
36- ->withHeader (" X-PCO-API-Version " , $ this ->apiVersion );
49+ ->withHeader (self :: X_PCO_API_VERSION_HEADER , $ this ->apiVersion );
3750 }
3851
3952 public function hostname (): string
@@ -48,6 +61,32 @@ public function setApiVersion(string $apiVersion): static
4861 return $ this ;
4962 }
5063
64+ /** @experimental This could change. Use with caution. */
65+ public function setAuthType (AuthType $ authType ): static
66+ {
67+ $ this ->authType = $ authType ;
68+
69+ return $ this ;
70+ }
71+
72+ /** @experimental This could change. Use with caution. */
73+ public function withBasicAuth (string $ clientId = "" , string $ clientSecret = "" ): static
74+ {
75+ $ this ->clientId = $ clientId ;
76+ $ this ->clientSecret = $ clientSecret ;
77+
78+ return $ this ->setAuthType (AuthType::Basic);
79+ }
80+
81+ /** @experimental This could change. Use with caution. */
82+ public function withToken (string $ token = "" ): static
83+ {
84+ $ this ->clientId = $ token ;
85+ $ this ->clientSecret = Str::empty ();
86+
87+ return $ this ->setAuthType (AuthType::Token);
88+ }
89+
5190 protected function processResponse (Response $ http ): ClientResponse
5291 {
5392 $ clientResponse = new ClientResponse ($ http );
0 commit comments