MockingMagician / coinbase-pro-sdk

Coinbase Pro API SDK - Communicate easily with the Coinbase Pro API in PHP
https://mockingmagician.github.io/coinbase-pro-sdk/
MIT License
22 stars 8 forks source link

Change how the API responses to the server? #20

Closed haexperts2020 closed 3 years ago

haexperts2020 commented 3 years ago

Thanks for the library, it was just what I needed.

I am using this with Laravel currently and when I get the API response I can dd($response); and see the array data from Coinbase. Is it possible to convert this easily to JSON?

I have tried to json_encode($response) and it returns ['','','','',''] blank brackets. I did some research and it found this reponse:

"This means that AccountData does not implement JsonSerializable and the members seen there are all private so they are not exposed when it gets converted to json. If you wrote those classes you should probably change then and if not then consult the documentation of whoever wrote them on how to get those fields exposed so you can write some custom code to covert them to an array"

This is just a simple $response = $api->accounts()->list(); by the way.

What do you think?

MockingMagician commented 3 years ago

Hi @haexperts2020,

$api->accounts()->list() returns an array of AccountData and it does not implement JsonSerializable, that's why you can't do json_encode($response).

If you want to get the json response directly from the server, without getting it in an object form, you can use listRaw() instead of list(), try this:

$response = $api->accounts()->listRaw();

dd($response);

In a general way each method giving on an endpoint of the api has a sister method ending by Raw and which makes it possible to recover the raw value returned by the API server of coinbase.

haexperts2020 commented 3 years ago

That makes a lot of sense, thanks for clearing that up. It worked for me. Just for my knowledge, what scenario would someone still want that data in object form as opposed to raw JSON?

Also is the Raw method explained in your docs?

MockingMagician commented 3 years ago

Hi @haexperts2020

In a way having an object in a programming language is always more natural than an array and especially more strict (precise).

Here is an example of what you can do with an object:

<?php

use MockingMagician\CoinbaseProSdk\CoinbaseFacade;

$api = CoinbaseFacade::createDefaultCoinbaseApi('', '', '', '');

$products = $api->products()->getProducts();
$accounts = $api->accounts()->list();

$productsWhereICanBuy = [];
$productsWhereICanSell = [];

foreach ($accounts as $account) {
    if (!$account->isTradingEnabled()) {
        continue;
    }
    if ($account->getAvailableFunds() === 0) {
        continue;
    }
    foreach ($products as $product) {
        $productPeer = explode('-', $product->getId());
        if ($productPeer[1] === $account->getId()) {
            $productsWhereICanBuy[] = $product;
            continue;
        }
        if ($productPeer[0] === $account->getId()) {
            $productsWhereICanSell[] = $product;
        }
    }
}

// Now, we have a list of products where we can do buy action
// And list of products where we can do sell action

Of course you could get the same thing with a json that you would transform into an array, but you would have to forget the possibility that your values are well casted and forget the autocompletion that your IDE can offer you. You can also imagine decorators that would offer additional functionality on the returned objects and there with a json or an array it would be impossible.

By the way, about Raw methods, no, the documentation doesn't mention this feature, but for testing and implementation purposes you can find the methods in the Connectivity classes. I'm thinking about the possibility to retrieve the raw value at least as an array like I implemented on the websocket side. If this feature is requested I will integrate it natively by documenting it.

Can you explain me your current need and in which context you use the library with an example of your code, if you don't use objects at all?

haexperts2020 commented 3 years ago

That's a really good example that I am going to use instead of making the objects raw.