1515use SharpAPI \SharpApiService \Dto \SharpApiSubscriptionInfo ;
1616use SharpAPI \SharpApiService \Enums \SharpApiJobStatusEnum ;
1717use SharpAPI \SharpApiService \Enums \SharpApiJobTypeEnum ;
18+ use Spatie \Url \Url ;
1819
1920class SharpApiService
2021{
@@ -42,7 +43,7 @@ public function __construct()
4243 $ this ->setApiBaseUrl (config ('sharpapi-client.base_url ' , 'https://sharpapi.com/api/v1 ' ));
4344 $ this ->setApiJobStatusPollingInterval ((int ) config ('sharpapi-client.api_job_status_polling_interval ' , 5 ));
4445 $ this ->setApiJobStatusPollingWait ((int ) config ('sharpapi-client.api_job_status_polling_wait ' , 180 ));
45- $ this ->setUserAgent (config ( ' sharpapi-client.user_agent ' ) );
46+ $ this ->setUserAgent (' SharpAPILaravelAgent/1.2.0 ' );
4647 }
4748
4849 /**
@@ -139,6 +140,7 @@ private function makeRequest(
139140
140141 private function parseStatusUrl (ResponseInterface $ response )
141142 {
143+ \Log::debug ('DBG parseStatusUrl: ' , [json_decode ($ response ->getBody ()->__toString (), true )['status_url ' ]]);
142144 return json_decode ($ response ->getBody ()->__toString (), true )['status_url ' ];
143145 }
144146
@@ -183,13 +185,24 @@ public function fetchResults(string $statusUrl): SharpApiJob
183185 } // otherwise wait a bit more and try again
184186 sleep ($ retryAfter );
185187 } while (true );
188+
186189 $ data = json_decode ($ response ->getBody ()->__toString (), true )['data ' ];
190+ \Log::debug ('DBG data: ' , $ data );
191+
192+ $ url = Url::fromString ($ statusUrl );
193+ if (count ($ url ->getSegments ()) == 5 ) { // shared job result URL
194+ \Log::debug ('DBG 5 segments: ' .$ url );
195+ $ result = json_decode ($ data ['attributes ' ]['result ' ]);
196+ } else { // 7 segments, 1-to-1 job to result url
197+ \Log::debug ('DBG 7 segments: ' .$ url );
198+ $ result = (object )$ data ['attributes ' ]['result ' ];
199+ }
187200
188201 return new SharpApiJob (
189202 id: $ data ['id ' ],
190203 type: $ data ['attributes ' ]['type ' ],
191204 status: $ data ['attributes ' ]['status ' ],
192- result: $ data [ ' attributes ' ][ ' result ' ] ?? null
205+ result: $ result ?? null
193206 );
194207 }
195208
@@ -539,7 +552,8 @@ public function summarizeText(
539552 string $ text ,
540553 ?string $ language = null ,
541554 ?int $ maxLength = null ,
542- ?string $ voiceTone = null
555+ ?string $ voiceTone = null ,
556+ ?string $ context = null
543557 ): string {
544558 $ response = $ this ->makeRequest (
545559 'POST ' ,
@@ -549,6 +563,7 @@ public function summarizeText(
549563 'language ' => $ language ,
550564 'max_length ' => $ maxLength ,
551565 'voice_tone ' => $ voiceTone ,
566+ 'context ' => $ context ,
552567 ]);
553568
554569 return $ this ->parseStatusUrl ($ response );
@@ -565,7 +580,8 @@ public function generateKeywords(
565580 string $ text ,
566581 ?string $ language = null ,
567582 ?int $ maxQuantity = null ,
568- ?string $ voiceTone = null
583+ ?string $ voiceTone = null ,
584+ ?string $ context = null
569585 ): string {
570586 $ response = $ this ->makeRequest (
571587 'POST ' ,
@@ -575,6 +591,7 @@ public function generateKeywords(
575591 'language ' => $ language ,
576592 'max_quantity ' => $ maxQuantity ,
577593 'voice_tone ' => $ voiceTone ,
594+ 'context ' => $ context ,
578595 ]);
579596
580597 return $ this ->parseStatusUrl ($ response );
0 commit comments