LendingClub Java Client

https://github.com/thysmichels/lendingclub-java-client

The minimum requirements to get started using LendingClub API:
1. Setup wsimport maven plugin
2. Run mvn clean install to generate LendingClub API Java classes from wsdl
3. Create basic LendingClub Main Class
4. Output

1. Setup wsimport maven plugin

         <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <wsdlUrls><wsdlUrl>https://www.lendingclub.com/ws/1.4?wsdl</wsdlUrl></wsdlUrls>
                <verbose>true</verbose>
            </configuration>
        </plugin>

2. Run mvn clean install to generate LendingClub API Java classes from wsdl

mvn clean install
...

[INFO] --- jaxws-maven-plugin:2.3:wsimport (default) @ lendingclub-java-client ---
[INFO] Processing: https://www.lendingclub.com/ws/1.4?wsdl
[INFO] jaxws:wsimport args: [-keep, -s, /Users/tmichels/git/lendingclub-java-client/target/generated-sources/wsimport, -d, /Users/tmichels/git/lendingclub-java-client/target/classes, -verbose, -encoding, UTF-8, -Xnocompile, "https://www.lendingclub.com/ws/1.4?wsdl"]
parsing WSDL...



Generating code...

com/lendingclub/ws/BadArgumentException.java
com/lendingclub/ws/BadArgumentExceptionInfo.java
com/lendingclub/ws/BrowseLoansResult.java
com/lendingclub/ws/CreatePortfolioResult.java
com/lendingclub/ws/CreditInfo.java
com/lendingclub/ws/ExcessiveRequestsException.java
com/lendingclub/ws/ExcessiveRequestsExceptionInfo.java
com/lendingclub/ws/GetPortfoliosResult.java
com/lendingclub/ws/HomeOwnership.java
com/lendingclub/ws/IncomeVerification.java
com/lendingclub/ws/InvalidCredentialsException.java
com/lendingclub/ws/InvalidCredentialsExceptionInfo.java
com/lendingclub/ws/LendingClubV14.java
com/lendingclub/ws/LendingClubV14_Service.java
com/lendingclub/ws/LoanBrowseLoans.java
com/lendingclub/ws/LoanBrowseLoansResponse.java
com/lendingclub/ws/LoanGrade.java
com/lendingclub/ws/LoanListing.java
com/lendingclub/ws/LoanPurpose.java
com/lendingclub/ws/LoanSubGrade.java
com/lendingclub/ws/MissingCredentialsException.java
com/lendingclub/ws/MissingCredentialsExceptionInfo.java
com/lendingclub/ws/ObjectFactory.java
com/lendingclub/ws/Order.java
com/lendingclub/ws/OrderConfirmation.java
com/lendingclub/ws/OrderCreatePortfolio.java
com/lendingclub/ws/OrderCreatePortfolioResponse.java
com/lendingclub/ws/OrderExecutionStatus.java
com/lendingclub/ws/OrderGetPortfolios.java
com/lendingclub/ws/OrderGetPortfoliosResponse.java
com/lendingclub/ws/OrderInstruct.java
com/lendingclub/ws/OrderInstructConfirmation.java
com/lendingclub/ws/OrderSubmitOrders.java
com/lendingclub/ws/OrderSubmitOrdersResponse.java
com/lendingclub/ws/Portfolio.java
com/lendingclub/ws/PortfolioResult.java
com/lendingclub/ws/ReviewStatus.java
com/lendingclub/ws/SubmitOrderResult.java
com/lendingclub/ws/SystemGetVersion.java
com/lendingclub/ws/SystemGetVersionResponse.java
com/lendingclub/ws/UnauthorizedWebserviceUserException.java
com/lendingclub/ws/UnauthorizedWebserviceUserExceptionInfo.java
com/lendingclub/ws/VersionInfo.java
com/lendingclub/ws/VersionStatus.java
com/lendingclub/ws/package-info.java

3. Create basic LendingClub Main Class

package com.lendingclub.main;

import com.lendingclub.exception.WebRequestException;
import com.lendingclub.ws.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

/**
 * Created by tmichels on 8/21/14.
 */
public class Main {

   private static final String LENDINGCLUB_API_KEY = "PASTE_YOUR_API_KEY_HERE";

    public static void main(String[] args) throws WebRequestException, UnauthorizedWebserviceUserException, ExcessiveRequestsException, InvalidCredentialsException, MissingCredentialsException {
        final String endpointUrl = "https://api.lendingclub.com:443/ws/1.4";
        Map<String, List<String>> headers = new HashMap<String,List<String>>();
        headers.put("Authorization", Collections.singletonList(LENDINGCLUB_API_KEY));
        LendingClubV14_Service lcSvc = new LendingClubV14_Service();
        LendingClubV14 lcWs = lcSvc.getLendingClubV14Port();
        Map<String, Object> requestContext = ((BindingProvider) lcWs).getRequestContext();
        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl);

        BrowseLoansResult browseLoansResult = lcWs.loanBrowseLoans(true);
        for (LoanListing ll : browseLoansResult.getLoans()){
            System.out.println("Loan: " + ll.getTitle() + " " + ll.getGrade());
        }
    }
}

4. Output

Loan: Debt consolidation B
Loan: Debt consolidation D
Loan: Credit card refinancing A
Loan: Credit card refinancing D

Using LendingClub Java Client library
1. LendingClub API Connection Config
2. LendingClub Client Service
3. LendingClub Client Main Class
4. LendingClub Client Config Test
5. LendingClub Client Tests

1. LendingClub API Connection Config

package com.lendingclub.config;

import com.lendingclub.ws.LendingClubV14;
import com.lendingclub.ws.LendingClubV14_Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import java.util.*;

/**
 * Created by tmichels on 8/21/14.
 */

@Configuration
@PropertySource("classpath:/system.properties")
public class ConnectionConfig {

    @Value("${lendingclub_api_key}")
    private String lendingclubKey;

    @Value("${lendingclub_api_url:https://api.lendingclub.com:443/ws/1.4}")
    private String lendingclubEndpoint;

    @Bean
    public LendingClubV14 lcConnection(){

        Map<String, List<String>> headers = new HashMap<String,List<String>>();
        headers.put("Authorization", Arrays.asList(lendingclubKey));

        LendingClubV14_Service lcSvc = new LendingClubV14_Service();
        LendingClubV14 lcWs = lcSvc.getLendingClubV14Port();
        Map<String, Object> requestContext = ((BindingProvider) lcWs).getRequestContext();

        // 1 minute for connection
        ((BindingProvider) lcWs).getRequestContext().put("com.sun.xml.ws.connect.timeout", 1 * 60 * 1000);
        // 1 minute for request
        ((BindingProvider) lcWs).getRequestContext().put("com.sun.xml.ws.request.timeout", 1 * 60 * 1000);

        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, lendingclubEndpoint);
        return lcWs;
    }
}

2. LendingClub Client Service

@Service
public class LendingClubClient {

    @Autowired
    private LendingClubV14 lcConnection;

    public BrowseLoansResult getLoans(boolean recentlyListed) throws Exception {
        return lcConnection.loanBrowseLoans(!recentlyListed);
    }

    public VersionInfo getSystemVersion() throws ExcessiveRequestsException {
        return lcConnection.systemGetVersion();
    }

    public void submitOrders(List<OrderInstruct> orderInstructList) throws Exception {
        lcConnection.orderSubmitOrders(orderInstructList);
    }

    public GetPortfoliosResult getPortfolios(Long portfolioId) throws Exception {
        return lcConnection.orderGetPortfolios(portfolioId);
    }

    public CreatePortfolioResult orderCreatePortfolio(Portfolio portfolio) throws Exception {
        return lcConnection.orderCreatePortfolio(portfolio);
    }
}

3. LendingClub Client Main Class

public class MainSpring {

    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.scan("com.lendingclub");
        applicationContext.refresh();

        LendingClubClient loanService = applicationContext.getBean(LendingClubClient.class);

        BrowseLoansResult browseLoansResult = loanService.getLoans(true);
        for (LoanListing loanListing : browseLoansResult.getLoans()){
            System.out.println("Loans: " + loanListing.getTitle() + " " + loanListing.getGrade());
        }
    }
}

4. Lending Club Client Config Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WSConnectionConfig.class, ClientConfig.class} , loader = AnnotationConfigContextLoader.class)
public class ConnectionConfigTest {

    @Autowired
    LendingClubV14 lcConnection;

    @Test
    public void testlcWSConnection() throws ExcessiveRequestsException {
        Assert.assertEquals(lcConnection.systemGetVersion().getNumber(), "1.4");
        Assert.assertEquals(lcConnection.systemGetVersion().getStatus().value(), "ACTIVE");
    }
}

5. Lending Club Client Tests

package com.lendingclub.client;

import com.lendingclub.ClientConfig;
import com.lendingclub.config.WSConnectionConfig;
import com.lendingclub.ws.Order;
import com.lendingclub.ws.OrderInstruct;
import com.lendingclub.ws.Portfolio;
import com.lendingclub.ws.SubmitOrderResult;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Arrays;

/**
 * Created by tmichels on 8/22/14.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WSConnectionConfig.class, ClientConfig.class, LendingClubClient.class} , loader = AnnotationConfigContextLoader.class)
public class LendingClubClientTest {

    @Autowired
    LendingClubClient lcClient;

    @Test
    public void testGetLoans() throws Exception {
        Assert.assertNotNull(lcClient.getLoans(true));
    }

    @Test
    public void testGetPortfolios() throws Exception {
        Assert.assertNotNull(lcClient.getPortfolios(new Long(6817950)));
    }

    @Test
    public void testOrderCreatePortfolio() throws Exception {
        Portfolio portfolio = new Portfolio();
        portfolio.setAid(new Long(6817950));
        portfolio.setPortfolioDescription("Dummy portfolio description");
        portfolio.setPortfolioName("Dummy portfilio");
        Assert.assertNotNull(lcClient.orderCreatePortfolio(portfolio));
    }

    @Test
    public void testSubmitOrders() throws Exception {
        OrderInstruct orderInstruct = new OrderInstruct();
        orderInstruct.setAid(new Long(6817950));
        lcClient.submitOrders(Arrays.asList(orderInstruct));
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: