Commits

Sam Adams  committed dc8cd70

Extract interface for ChempoundClient

  • Participants
  • Parent commits 707cda4

Comments (0)

Files changed (3)

File src/main/java/net/chempound/client/cli/ChempoundClientCLI.java

 
     private int getMaxCommandLength() {
         int max = -1;
-        for (String command : commands.keySet()) {
+        for (final String command : commands.keySet()) {
             max = Math.max(max, command.length());
         }
         return max;

File src/main/java/net/chempound/client/cli/command/CreateCollectionCommand.java

 import net.chempound.client.ChempoundClient;
 import net.chempound.client.CreateCollectionRequest;
 import net.chempound.client.CreateCollectionResponse;
+import net.chempound.client.DefaultChempoundClient;
 import net.chempound.client.cli.options.CreateCollectionOptions;
 import net.chempound.client.cli.options.GlobalOptions;
 
  */
 public class CreateCollectionCommand implements Command<CreateCollectionOptions> {
 
-    private final CreateCollectionOptions options = new CreateCollectionOptions();
+    private final CreateCollectionOptions options;
+    private final ChempoundClient client;
+
+    public CreateCollectionCommand() {
+        this(new CreateCollectionOptions());
+    }
+
+    protected CreateCollectionCommand(final CreateCollectionOptions options) {
+        this(options, new DefaultChempoundClient());
+    }
+
+    protected CreateCollectionCommand(final CreateCollectionOptions options, final ChempoundClient client) {
+        this.options = options;
+        this.client = client;
+    }
 
     @Override
     public void invoke(final GlobalOptions globalOptions) {
-        final ChempoundClient chempoundClient = new ChempoundClient();
         if (options.args == null || options.args.isEmpty()) {
             throw new RuntimeException("Title must be specified");
         }
 
         final CreateCollectionResponse response;
         try {
-            response = chempoundClient.createCollection(request);
+            response = client.createCollection(request);
             System.out.println("Collection created: " + response.getUri());
         } catch (Exception e) {
             e.printStackTrace();

File src/main/java/net/chempound/client/cli/command/DepositCommand.java

 package net.chempound.client.cli.command;
 
 import net.chempound.client.ChempoundClient;
+import net.chempound.client.DefaultChempoundClient;
 import net.chempound.client.DepositItemRequest;
 import net.chempound.client.DepositItemResponse;
 import net.chempound.client.cli.options.DepositOptions;
  */
 public class DepositCommand implements Command<DepositOptions> {
 
-    protected final DepositOptions depositOptions;
-    protected final ChempoundClient swordClient;
+    protected final DepositOptions options;
+    protected final ChempoundClient client;
 
     public DepositCommand() {
         this(new DepositOptions());
     }
 
-    protected DepositCommand(final DepositOptions depositOptions) {
-        this(depositOptions, new ChempoundClient());
+    protected DepositCommand(final DepositOptions options) {
+        this(options, new DefaultChempoundClient());
     }
 
-    protected DepositCommand(final DepositOptions depositOptions, final ChempoundClient swordClient) {
-        this.depositOptions = depositOptions;
-        this.swordClient = swordClient;
+    protected DepositCommand(final DepositOptions options, final ChempoundClient client) {
+        this.options = options;
+        this.client = client;
     }
 
     @Override
     public DepositOptions getOptions() {
-        return depositOptions;
+        return options;
     }
 
     @Override
 
         request.withUri(URI.create(globalOptions.repository));
         try {
-            final DepositItemResponse depositReceipt = swordClient.depositItem(request);
+            final DepositItemResponse depositReceipt = client.depositItem(request);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
     protected void setTitle(final DepositItemRequest request) {
-        if (depositOptions.title != null) {
-            request.withTitle(depositOptions.title);
+        if (options.title != null) {
+            request.withTitle(options.title);
         }
     }
 
     protected void setSlug(final DepositItemRequest request) {
-        if (depositOptions.slug != null) {
-            request.withSlug(depositOptions.slug);
+        if (options.slug != null) {
+            request.withSlug(options.slug);
         }
     }
 
     protected void attachFiles(final GlobalOptions globalOptions, final DepositItemRequest request) {
-        if (depositOptions.files != null) {
-            for (final String filename : depositOptions.files) {
+        if (options.files != null) {
+            for (final String filename : options.files) {
                 final File file = globalOptions.workingDirectory == null ? new File(filename) : new File(globalOptions.workingDirectory, filename);
                 final String depositFilename = FilenameUtils.getName(filename);
                 final LocalResource resource = new LocalFileResource(depositFilename, file);