Skip to content

Commit

Permalink
Merge pull request #5 from stitch-digital/johntrickett86-patch-1
Browse files Browse the repository at this point in the history
Update LaravelSimproApi.php
  • Loading branch information
johntrickett86 authored Oct 10, 2024
2 parents 4b2ef43 + 9889ce9 commit be932a0
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 21 deletions.
110 changes: 108 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,122 @@ Laravel Simpro API can be installed using composer:
composer require stitch-digital/laravel-simpro-api
```

Then run the following command to install the package:
Then publish the config file:

```bash
php artisan laravel-simpro:install
php artisan vendor:publish --tag="simpro-api-config"
```

This will publish the configuration file to `config/simpro-api.php` where you can configure your settings:

```php
return [

/*
|--------------------------------------------------------------------------
| Simpro Base URL
|--------------------------------------------------------------------------
|
| Set the base URL for the Simpro API. This option is not required when
| using a multi-tenancy setup, as values are passed in the constructor.
|
*/

'base_url' => env('SIMPRO_BASE_URL'),

/*
|--------------------------------------------------------------------------
| Simpro API Key
|--------------------------------------------------------------------------
|
| Set the API key for the Simpro API. This option is not required when
| using a multi-tenancy setup, as values are passed in the constructor.
|
*/

'api_key' => env('SIMPRO_API_KEY'),

/*
|--------------------------------------------------------------------------
| Cache Configuration
|--------------------------------------------------------------------------
|
| Enable or disable caching for Simpro GET requests. The cache driver can
| be set to any of the Laravel cache drivers. The cache expiry time is
| set in seconds.
|
*/

'cache' => [
'enabled' => 'true',
'driver' => 'database',
'expire' => 120,
],

/*
|--------------------------------------------------------------------------
| Rate Limit Configuration
|--------------------------------------------------------------------------
|
| Set the rate limit for Simpro requests. The rate limit is set per second
| and the threshold is the percentage of the rate limit that is accepted.
| The threshold must be a number between 0 and 1 (e.g. 0.5 for 50%).
|
| The default rate limit is as per the Simpro API documentation.
|
*/

'rate_limit' => [
'enabled' => true,
'per_second' => 10,
'driver' => 'database',
'threshold' => 0.8,
],

/*
|--------------------------------------------------------------------------
| Global Retry Configuration
|--------------------------------------------------------------------------
|
| Set the number of retries for all requests. This can still be overridden
| on a per-request basis, by chaining sendAndRetry() to the request:
|
| Example:
| $response = $connector->sendAndRetry($request, 1);
|
*/

'global_retries' => 3, // Set to null to disable global retries

];
```

You will need to add the following environment variables to your `.env` file:

```bash
SIMPRO_BASE_URL=https://your-build-url.simprosuite.com
SIMPRO_API_KEY=your-api-key
```

This package is built using Saloon. Check out their [documentation here](https://docs.saloon.dev/).

## Usage

To use the package, you can use the Simpro facade to make requests to the API:

```php
use StitchDigital\LaravelSimproApi\Facades\Simpro;
use StitchDigital\LaravelSimproApi\Requests\Info\GetInfo;

$response = Simpro::send(new GetInfo())->json();
```

For a full list of available requests, use the following command:

```bash
php artisan simpro:list-requests
```

This package is still in development and not all API endpoints are available yet. We are working on this package regularly as we use it for client projects, with regular releases to add more requests. If you see a missing request, please feel free to create a pull request and contribute!

### Changelog
Expand Down
56 changes: 45 additions & 11 deletions src/Commands/LaravelSimproApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,61 @@
namespace StitchDigital\LaravelSimproApi\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Helper\Table;

class LaravelSimproApiCommand extends Command
{
public $signature = 'simpro:install';
protected $signature = 'simpro:list-requests';

public $description = 'Install the Laravel Simpro API package';
protected $description = 'List all available requests in the Simpro package';

public function handle(): int
{
$this->comment('Publishing configuration...');
$this->info('Listing all available requests in the Simpro package:');

// Publish the config file
Artisan::call('vendor:publish', [
'--provider' => 'StitchDigital\\LaravelSimproApi\\LaravelSimproApiServiceProvider',
'--tag' => 'config',
]);
// Define the path to the Requests folder
$requestPath = base_path('vendor/stitchdigital/laravel-simpro-api/src/Requests');

$this->info('Configuration published.');
// Check if the directory exists
if (! is_dir($requestPath)) {
$this->error('Requests directory not found.');

$this->comment('Installation complete.');
return self::FAILURE;
}

// Create a Filesystem instance to work with files
$filesystem = new Filesystem;

// Get all PHP files in the Requests directory recursively
$files = $filesystem->allFiles($requestPath);

// Create an array to store formatted paths
$rows = [];

// Loop through the files and format the output
foreach ($files as $file) {
// Get the relative path from the Requests folder and format it
$relativePath = str_replace([$requestPath.DIRECTORY_SEPARATOR, '.php'], '', $file->getPathname());

// Split the path into parts
$pathParts = explode(DIRECTORY_SEPARATOR, $relativePath);

// Combine subdirectory and filename into a formatted path
$subDirectory = implode(' / ', array_slice($pathParts, 0, -1));
$fileName = end($pathParts);

// Add the subdirectory and filename to the rows array
$rows[] = [$subDirectory, $fileName];
}

// Create a table to display the requests
$this->table(
['Directory', 'Request'], // Table headers
$rows // Table rows
);

$this->info('Request listing complete.');

return self::SUCCESS;
}
Expand Down
16 changes: 8 additions & 8 deletions src/LaravelSimproApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class LaravelSimproApi extends Connector implements Cacheable, HasPagination
public function __construct()
{
// Set the rate limiting flag from the config
$this->rateLimitingEnabled = config('simpro.rate_limit.enabled', true);
$this->rateLimitingEnabled = config('simpro-api.rate_limit.enabled', true);
}

/**
* The Base URL of the API
*/
public function resolveBaseUrl(): string
{
return config('simpro.base_url').'/api/v1.0';
return config('simpro-api.base_url').'/api/v1.0';
}

/**
Expand All @@ -67,7 +67,7 @@ protected function defaultConfig(): array
*/
protected function defaultAuth(): TokenAuthenticator
{
return new TokenAuthenticator(config('simpro.api_key'));
return new TokenAuthenticator(config('simpro-api.api_key'));
}

/**
Expand All @@ -79,8 +79,8 @@ protected function resolveLimits(): array
{
return [
Limit::allow(
config('simpro.rate_limit.per_second'),
threshold: config('simpro.rate_limit.threshold')
config('simpro-api.rate_limit.per_second'),
threshold: config('simpro-api.rate_limit.threshold')
)->everySeconds(1)->sleep(),
];
}
Expand All @@ -90,7 +90,7 @@ protected function resolveLimits(): array
*/
protected function resolveRateLimitStore(): RateLimitStore
{
return new LaravelCacheStore(Cache::store(config('simpro.rate_limit.driver')));
return new LaravelCacheStore(Cache::store(config('simpro-api.rate_limit.driver')));
}

/**
Expand Down Expand Up @@ -133,15 +133,15 @@ protected function handleExceededLimit(Limit $limit, PendingRequest $pendingRequ
*/
public function resolveCacheDriver(): Driver
{
return new LaravelCacheDriver(Cache::store(config('simpro.cache.driver')));
return new LaravelCacheDriver(Cache::store(config('simpro-api.cache.driver')));
}

/**
* Cache expiry time in seconds
*/
public function cacheExpiryInSeconds(): int
{
return config('simpro.cache.enabled') === false ? 0 : config('simpro.cache.expire');
return config('simpro-api.cache.enabled') === false ? 0 : config('simpro-api.cache.expire');
}

/**
Expand Down

0 comments on commit be932a0

Please sign in to comment.