Skip to content

Commit 3118015

Browse files
committed
Initial commit - SharpAPI Laravel package
Laravel package for SharpAPI.com integration. Part of the SharpAPI Laravel SDK collection.
0 parents  commit 3118015

File tree

8 files changed

+453
-0
lines changed

8 files changed

+453
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/vendor/
2+
node_modules/
3+
npm-debug.log
4+
yarn-error.log
5+
6+
storage/*.key
7+
.env
8+
Homestead.yaml
9+
Homestead.json
10+
/.vagrant
11+
.phpunit.result.cache
12+
.idea
13+
14+
build
15+
composer.lock
16+
.phpunit.result.cache
17+
.php-cs-fixer.cache
18+
coverage
19+
phpstan.neon

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-ecommerce-product-intro` will be documented in this file.
4+
5+
## 1.0.0 - 2024-06-06
6+
7+
- Initial release
8+
- Support for generating product introductions with customizable parameters
9+
- Comprehensive documentation and examples

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) SharpAPI <contact@sharpapi.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
![SharpAPI GitHub cover](https://sharpapi.com/sharpapi-github-laravel-bg.jpg "SharpAPI Laravel Client")
2+
3+
# AI Product Introduction Generator for Laravel
4+
5+
## 🚀 Leverage AI API to generate engaging product introductions for E-commerce applications.
6+
7+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/sharpapi/laravel-ecommerce-product-intro.svg?style=flat-square)](https://packagist.org/packages/sharpapi/laravel-ecommerce-product-intro)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/sharpapi/laravel-ecommerce-product-intro.svg?style=flat-square)](https://packagist.org/packages/sharpapi/laravel-ecommerce-product-intro)
9+
10+
Check the details at SharpAPI's [E-commerce API](https://sharpapi.com/en/catalog/ai/e-commerce) page.
11+
12+
---
13+
14+
## Requirements
15+
16+
- PHP >= 8.1
17+
- Laravel >= 9.0
18+
19+
---
20+
21+
## Installation
22+
23+
Follow these steps to install and set up the SharpAPI Laravel Product Introduction Generator package.
24+
25+
1. Install the package via `composer`:
26+
27+
```bash
28+
composer require sharpapi/laravel-ecommerce-product-intro
29+
```
30+
31+
2. Register at [SharpAPI.com](https://sharpapi.com/) to obtain your API key.
32+
33+
3. Set the API key in your `.env` file:
34+
35+
```bash
36+
SHARP_API_KEY=your_api_key_here
37+
```
38+
39+
4. **[OPTIONAL]** Publish the configuration file:
40+
41+
```bash
42+
php artisan vendor:publish --tag=sharpapi-ecommerce-product-intro
43+
```
44+
45+
---
46+
## Key Features
47+
48+
- **AI-Powered Product Introduction Generation**: Efficiently create engaging product descriptions for your e-commerce platform.
49+
- **Multi-language Support**: Generate product introductions in multiple languages.
50+
- **Customizable Length**: Control the length of the generated product introduction.
51+
- **Voice Tone Control**: Set your preferred writing style for the product introduction.
52+
- **Robust Polling for Results**: Polling-based API response handling with customizable intervals.
53+
- **API Availability and Quota Check**: Check API availability and current usage quotas with SharpAPI's endpoints.
54+
55+
---
56+
57+
## Usage
58+
59+
You can inject the `EcommerceProductIntroService` class to access product introduction generation functionality. For best results, especially with batch processing, use Laravel's queuing system to optimize job dispatch and result polling.
60+
61+
### Basic Workflow
62+
63+
1. **Dispatch Job**: Send product data to the API using `generateProductIntro`, which returns a status URL.
64+
2. **Poll for Results**: Use `fetchResults($statusUrl)` to poll until the job completes or fails.
65+
3. **Process Result**: After completion, retrieve the results from the `SharpApiJob` object returned.
66+
67+
> **Note**: Each job typically takes a few seconds to complete. Once completed successfully, the status will update to `success`, and you can process the results as JSON, array, or object format.
68+
69+
---
70+
71+
### Controller Example
72+
73+
Here is an example of how to use `EcommerceProductIntroService` within a Laravel controller:
74+
75+
```php
76+
<?php
77+
78+
namespace App\Http\Controllers;
79+
80+
use GuzzleHttp\Exception\GuzzleException;
81+
use SharpAPI\EcommerceProductIntro\EcommerceProductIntroService;
82+
83+
class ProductController extends Controller
84+
{
85+
protected EcommerceProductIntroService $productIntroService;
86+
87+
public function __construct(EcommerceProductIntroService $productIntroService)
88+
{
89+
$this->productIntroService = $productIntroService;
90+
}
91+
92+
/**
93+
* @throws GuzzleException
94+
*/
95+
public function generateProductIntro(string $productData)
96+
{
97+
$statusUrl = $this->productIntroService->generateProductIntro(
98+
$productData,
99+
'English', // optional language
100+
300, // optional maximum length
101+
'Enthusiastic' // optional voice tone
102+
);
103+
104+
$result = $this->productIntroService->fetchResults($statusUrl);
105+
106+
return response()->json($result->getResultJson());
107+
}
108+
}
109+
```
110+
111+
### Handling Guzzle Exceptions
112+
113+
All requests are managed by Guzzle, so it's helpful to be familiar with [Guzzle Exceptions](https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions).
114+
115+
Example:
116+
117+
```php
118+
use GuzzleHttp\Exception\ClientException;
119+
120+
try {
121+
$statusUrl = $this->productIntroService->generateProductIntro('Sony PlayStation 5 with 1TB SSD', 'English', 300);
122+
} catch (ClientException $e) {
123+
echo $e->getMessage();
124+
}
125+
```
126+
127+
---
128+
129+
## Optional Configuration
130+
131+
You can customize the configuration by setting the following environment variables in your `.env` file:
132+
133+
```bash
134+
SHARP_API_KEY=your_api_key_here
135+
SHARP_API_JOB_STATUS_POLLING_WAIT=180
136+
SHARP_API_JOB_STATUS_USE_POLLING_INTERVAL=true
137+
SHARP_API_JOB_STATUS_POLLING_INTERVAL=10
138+
SHARP_API_BASE_URL=https://sharpapi.com/api/v1
139+
```
140+
141+
---
142+
143+
## Product Introduction Data Format Example
144+
145+
```json
146+
{
147+
"data": {
148+
"type": "api_job_result",
149+
"id": "fb0a5ab0-54fe-4283-9a33-ac491962c778",
150+
"attributes": {
151+
"status": "success",
152+
"type": "ecommerce_product_intro",
153+
"result": {
154+
"product_intro": "Experience unparalleled gaming with the Razer Blade 16, featuring NVIDIA RTX 4090, 13th Gen Intel i9, dual-mode 4K/FHD display, 32GB RAM, 2TB SSD, and a compact GaN charger."
155+
}
156+
}
157+
}
158+
}
159+
```
160+
161+
---
162+
163+
## Support & Feedback
164+
165+
For issues or suggestions, please:
166+
167+
- [Open an issue on GitHub](https://github.com/sharpapi/laravel-ecommerce-product-intro/issues)
168+
- Join our [Telegram community](https://t.me/sharpapi_community)
169+
170+
---
171+
172+
## Changelog
173+
174+
Please see [CHANGELOG](CHANGELOG.md) for a detailed list of changes.
175+
176+
---
177+
178+
## Credits
179+
180+
- [A2Z WEB LTD](https://github.com/a2zwebltd)
181+
- [Dawid Makowski](https://github.com/makowskid)
182+
- Enhance your [Laravel AI](https://sharpapi.com/) capabilities!
183+
184+
---
185+
186+
## License
187+
188+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
189+
190+
---
191+
192+
## Follow Us
193+
194+
Stay updated with news, tutorials, and case studies:
195+
196+
- [SharpAPI on X (Twitter)](https://x.com/SharpAPI)
197+
- [SharpAPI on YouTube](https://www.youtube.com/@SharpAPI)
198+
- [SharpAPI on Vimeo](https://vimeo.com/SharpAPI)
199+
- [SharpAPI on LinkedIn](https://www.linkedin.com/products/a2z-web-ltd-sharpapicom-automate-with-aipowered-api/)
200+
- [SharpAPI on Facebook](https://www.facebook.com/profile.php?id=61554115896974)

composer.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"name": "sharpapi/laravel-ecommerce-product-intro",
3+
"description": "AI Product Introduction Generator for Laravel powered by SharpAPI.com",
4+
"keywords": [
5+
"sharpapi",
6+
"ai-powered",
7+
"ai capabilities",
8+
"api",
9+
"ai api",
10+
"api integration",
11+
"artificial intelligence",
12+
"natural language processing",
13+
"restful api",
14+
"php",
15+
"laravel",
16+
"software development",
17+
"e-commerce",
18+
"product descriptions",
19+
"marketing content",
20+
"product introduction"
21+
],
22+
"homepage": "https://github.com/sharpapi/laravel-ecommerce-product-intro",
23+
"license": "MIT",
24+
"authors": [
25+
{
26+
"name": "Dawid Makowski",
27+
"email": "contact@sharpapi.com",
28+
"role": "Developer"
29+
}
30+
],
31+
"require": {
32+
"php": "^8.1",
33+
"ext-json": "*",
34+
"guzzlehttp/guzzle": "^7.0",
35+
"laravel/framework": "^9.0|^10.0|^11.0|^12.0",
36+
"kongulov/interact-with-enum": "^1.0",
37+
"sharpapi/php-core": "^1.0",
38+
"spatie/url": "^2.4"
39+
},
40+
"require-dev": {
41+
"laravel/pint": "^1.0"
42+
},
43+
"autoload": {
44+
"psr-4": {
45+
"SharpAPI\\EcommerceProductIntro\\": "src/"
46+
}
47+
},
48+
"autoload-dev": {
49+
"psr-4": {
50+
"SharpAPI\\EcommerceProductIntro\\Tests\\": "tests/"
51+
}
52+
},
53+
"scripts": {
54+
"post-autoload-dump": "",
55+
"build": [
56+
],
57+
"start": [
58+
"Composer\\Config::disableProcessTimeout",
59+
"@composer run build"
60+
],
61+
"analyse": "vendor/bin/phpstan analyse",
62+
"test": "vendor/bin/pest",
63+
"test-coverage": "vendor/bin/pest --coverage",
64+
"format": "vendor/bin/pint"
65+
},
66+
"config": {
67+
"sort-packages": true,
68+
"allow-plugins": {
69+
"pestphp/pest-plugin": true,
70+
"phpstan/extension-installer": true
71+
}
72+
},
73+
"extra": {
74+
"laravel": {
75+
"providers": [
76+
"SharpAPI\\EcommerceProductIntro\\EcommerceProductIntroProvider"
77+
]
78+
}
79+
},
80+
"minimum-stability": "dev",
81+
"prefer-stable": true
82+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'api_key' => env('SHARP_API_KEY'),
7+
'base_url' => env('SHARP_API_BASE_URL', 'https://sharpapi.com/api/v1'), // as ENV is mock server needed
8+
// how long (in seconds) the client should wait in polling mode for results
9+
'api_job_status_polling_wait' => env('SHARP_API_JOB_STATUS_POLLING_WAIT', 180),
10+
// how many seconds the client should wait between each result request
11+
// usually Retry-After header is used (default 10s), this value won't have an effect unless
12+
// api_job_status_use_polling_interval is set to TRUE
13+
'api_job_status_polling_interval' => env('SHARP_API_JOB_STATUS_POLLING_INTERVAL', 10),
14+
'api_job_status_use_polling_interval' => env('SHARP_API_JOB_STATUS_USE_POLLING_INTERVAL', false),
15+
// for affiliate program members use
16+
];
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SharpAPI\EcommerceProductIntro;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
9+
/**
10+
* @api
11+
*/
12+
class EcommerceProductIntroProvider extends ServiceProvider
13+
{
14+
/**
15+
* Bootstrap the application services.
16+
*/
17+
public function boot(): void
18+
{
19+
if ($this->app->runningInConsole()) {
20+
$this->publishes([
21+
__DIR__.'/../config/sharpapi-ecommerce-product-intro.php' => config_path('sharpapi-ecommerce-product-intro.php'),
22+
], 'sharpapi-ecommerce-product-intro');
23+
}
24+
}
25+
26+
/**
27+
* Register the application services.
28+
*/
29+
public function register(): void
30+
{
31+
// Merge the package configuration with the app configuration.
32+
$this->mergeConfigFrom(
33+
__DIR__.'/../config/sharpapi-ecommerce-product-intro.php', 'sharpapi-ecommerce-product-intro'
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)