Skip to content

Commit

Permalink
chore: upgrade easypost-php and adjust client initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Jul 19, 2024
1 parent be677c2 commit 28d9580
Show file tree
Hide file tree
Showing 14 changed files with 616 additions and 543 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# CHANGELOG

## Next Release
## v0.12.0 (2024-07-18)

- Upgrades `easypost-php` from v6 to v7
- Moves the EasyPostClient initialization from middleware to each function call and passes down the user's API key from the session instead
- Switches from Font Awesome to Bootstrap Icons
- Bumps deps

## v0.11.0 (2024-06-10)

Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -30,7 +31,7 @@ public function createAddress(Request $request): RedirectResponse
'email' => 'nullable|string',
]);

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$address = $client->address->create(
Expand Down Expand Up @@ -62,7 +63,7 @@ public function createAddress(Request $request): RedirectResponse
*/
public function retrieveAddress(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$json = $client->address->retrieve($id);
Expand All @@ -81,7 +82,7 @@ public function retrieveAddress(Request $request, string $id): View|RedirectResp
*/
public function retrieveAddresses(Request $request): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$json = $client->address->all();
Expand Down
5 changes: 3 additions & 2 deletions src/app/Http/Controllers/CarrierController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand All @@ -18,7 +19,7 @@ class CarrierController extends Controller
*/
public function retrieveCarrier(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$carrier = $client->carrierAccount->retrieve($id);
Expand All @@ -37,7 +38,7 @@ public function retrieveCarrier(Request $request, string $id): View|RedirectResp
*/
public function retrieveCarriers(Request $request): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$carriers = $client->carrierAccount->all();
Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/InsuranceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function createInsurance(Request $request): RedirectResponse
'amount' => 'required|string|max:5000',
]);

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
if ($request->input('to_address') != null) {
Expand Down Expand Up @@ -125,7 +126,7 @@ public function createInsurance(Request $request): RedirectResponse
*/
public function retrieveInsurance(Request $request, string $id)
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$insurance = $client->insurance->retrieve($id);
Expand All @@ -144,7 +145,7 @@ public function retrieveInsurance(Request $request, string $id)
*/
public function retrieveInsurances(Request $request)
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$insurances = $client->insurance->all();
Expand Down
5 changes: 3 additions & 2 deletions src/app/Http/Controllers/ParcelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function createParcel(Request $request): RedirectResponse
]);
}

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
if ($request->input('predefined_package') != null) {
Expand Down Expand Up @@ -65,7 +66,7 @@ public function createParcel(Request $request): RedirectResponse
*/
public function retrieveParcel(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$parcel = $client->parcel->retrieve($id);
Expand Down
5 changes: 3 additions & 2 deletions src/app/Http/Controllers/RefundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand All @@ -18,7 +19,7 @@ class RefundController extends Controller
*/
public function retrieveRefund(Request $request, string $id)
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$refund = $client->refund->retrieve($id);
Expand All @@ -40,7 +41,7 @@ public function retrieveRefund(Request $request, string $id)
*/
public function retrieveRefunds(Request $request): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$refunds = $client->refund->all();
Expand Down
3 changes: 2 additions & 1 deletion src/app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function searchRecord(Request $request): View|RedirectResponse
{
$id = $request->input('id');
$idPrefix = substr($id, 0, strpos($id, '_'));
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$class = OBJECT_ID_PREFIXES[$idPrefix] ?? null;
Expand Down
15 changes: 8 additions & 7 deletions src/app/Http/Controllers/ShipmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function createShipment(Request $request): View|RedirectResponse
]);
}

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

if ($request->input('to_address') != null) {
try {
Expand Down Expand Up @@ -178,7 +179,7 @@ function ($a, $b) {
*/
public function retrieveShipment(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$shipment = $client->shipment->retrieve($id);
Expand All @@ -197,7 +198,7 @@ public function retrieveShipment(Request $request, string $id): View|RedirectRes
*/
public function retrieveShipments(Request $request): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$shipments = $client->shipment->all([
Expand All @@ -219,7 +220,7 @@ public function retrieveShipments(Request $request): View|RedirectResponse
*/
public function createRefund(Request $request, string $id): RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$client->shipment->refund($id);
Expand All @@ -240,7 +241,7 @@ public function createRefund(Request $request, string $id): RedirectResponse
*/
public function buyShipment(Request $request, string $id): RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$shipment = $client->shipment->buy(
Expand Down Expand Up @@ -295,7 +296,7 @@ public function buyStamp(Request $request): RedirectResponse
]);
}

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

if ($request->input('to_address') != null) {
try {
Expand Down Expand Up @@ -382,7 +383,7 @@ public function buyStamp(Request $request): RedirectResponse
*/
public function generateQrCodes(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$shipment = $client->shipment->generateForm($id, 'label_qr_code');
Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use EasyPost\EasyPostClient;
use EasyPost\Exception\Api\ApiException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand All @@ -22,7 +23,7 @@ public function createTracker(Request $request): View|RedirectResponse
'carrier' => 'nullable|string',
]);

$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$response = $client->tracker->create(
Expand All @@ -47,7 +48,7 @@ public function createTracker(Request $request): View|RedirectResponse
*/
public function retrieveTracker(Request $request, string $id): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$tracker = $client->tracker->retrieve($id);
Expand All @@ -66,7 +67,7 @@ public function retrieveTracker(Request $request, string $id): View|RedirectResp
*/
public function retrieveTrackers(Request $request): View|RedirectResponse
{
$client = $request->session()->get('client');
$client = new EasyPostClient($request->session()->get('apiKey'));

try {
$response = $client->tracker->all();
Expand Down
5 changes: 2 additions & 3 deletions src/app/Http/Middleware/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ public function handle($request, Closure $next)
try {
$apiKey = Crypt::decryptString(Auth::user()->api_key);
} catch (DecryptException $e) {
session()->flash('error', 'API Key could not be decrypted. Please update your key and try again.');
session()->flash('error', 'API key could not be decrypted. Please update your API key and try again.');
return back();
}

$client = new EasyPostClient($apiKey);
$request->session()->put(['client' => $client]);
$request->session()->put(['apiKey' => $apiKey]);

return $next($request);
}
Expand Down
2 changes: 1 addition & 1 deletion src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"require": {
"php": "^8.2",
"anhskohbo/no-captcha": "^3.6",
"easypost/easypost-php": "^6.0",
"easypost/easypost-php": "^7.0",
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9",
"laravel/ui": "^4.5",
Expand Down
Loading

0 comments on commit 28d9580

Please sign in to comment.