irufus / gdax-java

Java based wrapper for Coinbase Pro (Formerly known as GDAX API and Coinbase Exchange API)
MIT License
177 stars 131 forks source link

Example / Quick Start #35

Closed hkammerl closed 4 years ago

hkammerl commented 6 years ago

I'm not used to spring, so it would be very helpful to have an example, or a small sort of quick-start explanation how to get started using the your API. I managed to build it using the Eclipse IDE, and also to run the unit tests, but struggling now how to start. It would be helpful to have a description of - for example - just read my account-data. thx in advance!

robevansuk commented 6 years ago

Here's the info/example you need:

@Component
public class MyApplicationClassThatDoesStuff {

AccountService accountService;

@Autowired
public MyApplicationClassThatDoesStuff(AccountService accountService){
    this.accountService = accountService;
}

public void printMyAccountData(){
    List<Account> myAccounts = accountService.getAccounts();
    log.info(myAccounts.getProductId());
     ... etc.
}

Service classes are the ones you interact with to do stuff.

If you want MarketData, you Autowire the MarketDataService to the constructor of your "Component" you're creating, then you can call its methods freely. Same goes for any other Service class.

Example of my (poor) attempt at creating a live orderbook view that follows this example can be found here: https://github.com/irufus/gdax-java/blob/master/src/main/java/com/coinbase/exchange/api/gui/GuiFrame.java

This library only uses 3 of Springs annotations:

If we didn't use these annotations, the code would be much more tightly coupled and harder to use.

CarlClaesson commented 6 years ago

I am a beginner so maybe this is a very stupid question but how do you write your main class to actually use the method printMyAccountData()?

robevansuk commented 6 years ago

If you want it all in a main class refer to this example. https://stackoverflow.com/questions/48247177/gdax-api-bad-request-400/48382532#48382532

If you want to do this, you can write a class something like.

package com.coinbase.exchange.api;

import com.coinbase.exchange.api.accounts.Account;
import com.coinbase.exchange.api.accounts.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;

public class MyImplementationWithoutSpringAnnotations {

    private static final Logger log = LoggerFactory.getLogger(MyImplementationWithoutSpringAnnotations.class);

    private static AccountService accountService;

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(GdaxApiApplication.class)
                .headless(false).run(args);
        accountService = ctx.getBean(AccountService.class);
        List<Account> myAccounts = accountService.getAccounts();
        for (Account account: myAccounts) {
            log.info("My accounts: productId: {}, balance: {}", account.getCurrency(), account.getBalance());
        }
    }
}

I'd say you'll likely end up with tightly coupled code though and it will be a nightmare to configure/test/maintain. Spring is the easier option if you learn how it works.

CarlClaesson commented 6 years ago

Thanks for the answer but I cant get it to work. The first error I get is that the compiler says that the method getInstance is undefined for the type LoggerFactory.

robevansuk commented 6 years ago

try again - I've updated with full code I've tried in my IDE.

CarlClaesson commented 6 years ago

Thank you very much, now it starts to run but now I get an error that says "Could not resolve placeholder 'gdax.key' in value "${gdax.key}"". Does this mean I did not edit my application.yml properly?

robevansuk commented 6 years ago

you have an extra quotation mark in what you wrote above. Was that an error? "${gdax.key}"" Perhaps thats why? If you didnt set a value I suspect it would say something more like "invalid credentials"

CarlClaesson commented 6 years ago

I tried your stackoverflow example instead and got it to work, thank you very much :)