wetcat-studios / fortie

Laravel 5 package for Fortnox API
Apache License 2.0
19 stars 20 forks source link

FYI, tests with Laravel 7 Http::client = success #58

Open tanthammar opened 4 years ago

tanthammar commented 4 years ago

Fun fact:

Tested to connect to Fortnox api with Laravel 7 new Http::client. Might be of interest for someones fork or for future updates.

This works very well:

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Access-Token' => 'secret',
    'Client-Secret' => 'moresecret',
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
])->post('https://api.fortnox.se/3/invoices', [
    "Invoice" => [
        "InvoiceRows" => [
            [
                "DeliveredQuantity" => "10.00",
            ],
        ],
        "CustomerNumber" => "1",
    ],
]);

if ($response->successful()) {  // $response->ok() does not work
  $invoice = $response->json();
  dump($invoice);
}

// Determine if the response has a 400 level status code...
if ($response->clientError()) {
  dump($response->json());
};

// Determine if the response has a 500 level status code...
if ($response->serverError()) {
  dump('Oops! Something went wrong, please contact support');
}

Other nice features: https://laravel.com/docs/master/http-client#making-requests

If you run into cURL error 56 on mac os, I had to brew update && brew upgrade + php v 7.4.1

agoransson commented 4 years ago

A nice idea.

You could perhaps try to make the HTTP part generic, that way you could perhaps push the lib of your choice into fortie and limiting the amount of dependencies.

I think at the time I wrote this I was most comfortable with guzzle - I'm not entirely sure why I decided to bring it in.

agoransson commented 3 years ago

Also, what I thought about now when re-reading this is that the project should not be dependent on Laravel so that it can be used in non-Laravel projects. I'm writing up some kind of generic solution to this now, where the default client is Guzzle (because it's Guzzle=)) but at the same time allowing someone to define their own client implementation as well based on any HTTP implementation.

jongotlin commented 3 years ago

Yes, good idea to move Laravel related code to another library dependent on this. I use this library in a Symfony project and it works just fine (although the Guzzle client would rather be injected than created in a constructor).

agoransson commented 3 years ago

Yep, should definitely be injected.