[Bps-public-commit] storage-box branch, master, updated. 083e9386f42e206c96335b9e61d9fc5f2f06e9c4

Dave Goehrig dave at bestpractical.com
Fri Sep 9 13:23:41 EDT 2016


The branch, master has been updated
       via  083e9386f42e206c96335b9e61d9fc5f2f06e9c4 (commit)
      from  3f6a2e009294329dd87b69aa6d37746d31054ed9 (commit)

Summary of changes:
 META.json                                    |  1 +
 README.md                                    |  2 +-
 cpanfile                                     |  1 +
 lib/Storage/Box.pm                           | 14 ++++++++------
 lib/Storage/Box/Auth.pm                      |  3 ++-
 lib/Storage/Box/File.pm                      | 16 +++++++++++-----
 lib/Storage/Box/Folder.pm                    |  1 +
 lib/Storage/Box/Request.pm                   |  1 -
 scripts/auth_enterprise.pl                   |  0
 scripts/auth_user.pl                         |  0
 scripts/create_file.pl                       |  0
 scripts/create_user.pl                       |  0
 scripts/create_user2.pl                      | 21 +++++++++++++++++++++
 scripts/delete_file.pl                       |  0
 scripts/delete_user.pl                       |  0
 scripts/{delete_file.pl => download_file.pl} |  7 +++++--
 scripts/read_user.pl                         |  0
 scripts/update_user.pl                       |  0
 t/basic.t                                    |  1 -
 19 files changed, 51 insertions(+), 17 deletions(-)
 mode change 100644 => 100755 scripts/auth_enterprise.pl
 mode change 100644 => 100755 scripts/auth_user.pl
 mode change 100644 => 100755 scripts/create_file.pl
 mode change 100644 => 100755 scripts/create_user.pl
 create mode 100755 scripts/create_user2.pl
 mode change 100644 => 100755 scripts/delete_file.pl
 mode change 100644 => 100755 scripts/delete_user.pl
 copy scripts/{delete_file.pl => download_file.pl} (83%)
 mode change 100644 => 100755
 mode change 100644 => 100755 scripts/read_user.pl
 mode change 100644 => 100755 scripts/update_user.pl

- Log -----------------------------------------------------------------
commit 083e9386f42e206c96335b9e61d9fc5f2f06e9c4
Author: Dave Goehrig <dave at bestpractical.com>
Date:   Fri Sep 9 13:23:34 2016 -0400

    mostly working

diff --git a/META.json b/META.json
index 4ca2ce3..11ca7a5 100644
--- a/META.json
+++ b/META.json
@@ -42,6 +42,7 @@
             "Expect" : "1.15",
             "JSON" : "2.90",
             "Modern::Perl" : "1.20150127",
+            "Object::Simple" : "3.1702",
             "WWW::Curl" : "4.17",
             "perl" : "5.008005"
          }
diff --git a/README.md b/README.md
index ea06b60..33e5dc7 100644
--- a/README.md
+++ b/README.md
@@ -119,7 +119,7 @@ It provides a JWT authenticated cleint for a server side application.
 
 # AUTHOR
 
-Dave Goehrig <dave at dloh.org>
+Dave Goehrig <dave at dloh.org>
 
 # COPYRIGHT
 
diff --git a/cpanfile b/cpanfile
index df02af6..84c721e 100644
--- a/cpanfile
+++ b/cpanfile
@@ -1,5 +1,6 @@
 requires 'perl', '5.008005';
 
+requires 'Object::Simple','3.1702';
 requires 'Modern::Perl', '1.20150127';
 requires 'Crypt::JWT', '0.017';
 requires 'Expect', '1.15';
diff --git a/lib/Storage/Box.pm b/lib/Storage/Box.pm
index 4db248c..de655e4 100644
--- a/lib/Storage/Box.pm
+++ b/lib/Storage/Box.pm
@@ -48,6 +48,7 @@ It provides a JWT authenticated cleint for a server side application.
 has key_id => '';
 has enterprise_id => '';
 has public_key => '';
+has private_key => '';
 has password => '';
 has client_id => '';
 has client_secret => '';
@@ -81,7 +82,7 @@ sub authenticate_enterprise {
     my $self = shift;    
     $self->enterprise_auth('') if ($self->enterprise_auth and 
         $self->enterprise_auth->expired);
-    unless ($self->enterprise_auth) {
+     if ($self->enterprise_auth eq '') {
         $self->enterprise_auth( Storage::Box::Auth->new(
             key_id => $self->key_id,
             enterprise_id => $self->enterprise_id,
@@ -90,7 +91,7 @@ sub authenticate_enterprise {
             client_id => $self->client_id,
             client_secret => $self->client_secret
         ));
-        $self->enterprise_auth->request;
+        $self->enterprise_auth->enterprise->request;
     }
     $self;
 }
@@ -122,7 +123,7 @@ sub authenticate_user {
     my $self = shift;    
     $self->user_auth('') if ($self->user_auth and 
         $self->user_auth->expired);
-    unless($self->user_auth) {
+    if ($self->user_auth eq '') {
         $self->user_auth( Storage::Box::Auth->new(
             key_id => $self->key_id,
             user_id => $self->user_id,
@@ -131,7 +132,7 @@ sub authenticate_user {
             client_id => $self->client_id,
             client_secret => $self->client_secret
         ));
-        $self->user_auth->request;
+        $self->user_auth->user->request;
     }
     $self;
 }
@@ -153,7 +154,8 @@ sub create_user {
         auth => $self->enterprise_auth,
         name => $name
     );
-    $user->create;
+    $user->create();
+    $user->id;
 }
 
 =pod
@@ -232,7 +234,7 @@ sub create_file {
         name => $filename
     );
     $file->create;
-    $file;
+    $file->id;
 }
 
 =pod
diff --git a/lib/Storage/Box/Auth.pm b/lib/Storage/Box/Auth.pm
index 3d07e21..7c57f1c 100644
--- a/lib/Storage/Box/Auth.pm
+++ b/lib/Storage/Box/Auth.pm
@@ -54,6 +54,7 @@ has token => '';                        # OAuth2 token generated by request
 has expires => 0;
 
 =pod 
+
 B<generate_private_key()>
 
   Using openssl, this generates a 2048 bit aes256 private key file
@@ -237,7 +238,7 @@ sub request {
 }
 
 sub expired {
-    my $self = time;
+    my $self = shift;
     time >= $self->expires;
 }
 
diff --git a/lib/Storage/Box/File.pm b/lib/Storage/Box/File.pm
index 321ebf2..f775885 100644
--- a/lib/Storage/Box/File.pm
+++ b/lib/Storage/Box/File.pm
@@ -36,6 +36,7 @@ has parent => '0';      # id of the parent folder, 0 is top level default
 has auth => '';         # Storage::Box::Auth object
 
 =pod
+
 B<create($user_token,$name,$parent,$filename)>
 
   Creates a new File with the given name in the specified parent Folder.
@@ -51,9 +52,13 @@ sub create {
     $req->field('attributes',"{\"name\":\"" . $self->name . "\",\"parent\":{\"id\":\"" . $self->parent . "\"}}");
     $req->file($self->name);
     $req->post->request;
-    return $self unless ($req->code == 201);
+    return $self unless ($req->code == 201 || $req->code == 409);
     my $json = decode_json($req->body);
-    $self->id($json->{entries}[0]->{id});   # update our id
+    if ($req->code == 201) {
+	$self->id($json->{entries}[0]->{id});   # update our id
+    } elsif ($req->code == 409) {
+	$self->id($json->{context_info}->{conflicts}->{id});   # update our id to the existing file
+    }
     $self;
 }
 
@@ -67,11 +72,12 @@ B<download($token,$fileid)>
 
 sub download {
     my $self = shift;
+    my $file_id = $self->id;
     my $req = Storage::Box::Request->new(
-        url => "https://api.box.com/2.0/files/" . $self->id . "/content",
+        url => "https://api.box.com/2.0/files/$file_id/content",
         auth => $self->auth
     );
-    $req->get->perform;
+    $req->get->request;
     $req->body;
 }
  
@@ -81,7 +87,7 @@ sub delete {
         url => "https://api.box.com/2.0/files/" . $self->id,
         auth => $self->auth
     );
-    $req->delete->perform;
+    $req->delete->request;
     $req->code == 200 || $req->code == 404;
 }
 
diff --git a/lib/Storage/Box/Folder.pm b/lib/Storage/Box/Folder.pm
index 4bb6b31..0ed2079 100644
--- a/lib/Storage/Box/Folder.pm
+++ b/lib/Storage/Box/Folder.pm
@@ -39,6 +39,7 @@ has parent => '0';      # id of the parent folder, 0 is top level default
 has auth => '';         # Storage::Box::Auth object
 
 =pod
+
 B<create($user_token,$name,$parent,$filename)>
 
   Creates a new File with the given name in the specified parent Folder.
diff --git a/lib/Storage/Box/Request.pm b/lib/Storage/Box/Request.pm
index 7e8a711..ced0404 100644
--- a/lib/Storage/Box/Request.pm
+++ b/lib/Storage/Box/Request.pm
@@ -100,7 +100,6 @@ sub file {
     my $self = shift;
     my $filename = shift;
     $self->form( WWW::Curl::Form->new() ) unless $self->form;
-    $self->form->formadd('attributes',"{\"name\":\"" . $self->name . "\",\"parent\":{\"id\":\"" . $self->parent . "\"}}");
     $self->form->formaddfile($filename, 'attachment', "multipart/form-data");
     $self;
 }
diff --git a/scripts/auth_enterprise.pl b/scripts/auth_enterprise.pl
old mode 100644
new mode 100755
diff --git a/scripts/auth_user.pl b/scripts/auth_user.pl
old mode 100644
new mode 100755
diff --git a/scripts/create_file.pl b/scripts/create_file.pl
old mode 100644
new mode 100755
diff --git a/scripts/create_user.pl b/scripts/create_user.pl
old mode 100644
new mode 100755
diff --git a/scripts/create_user2.pl b/scripts/create_user2.pl
new file mode 100755
index 0000000..0334d6b
--- /dev/null
+++ b/scripts/create_user2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use lib 'lib';
+
+use Storage::Box;
+
+my ($username,$enterprise_id) = @ARGV;
+
+$enterprise_id ||= '2064336';
+
+my $box = Storage::Box->new(
+	key_id => "vmqys6db",
+	enterprise_id => $enterprise_id,
+	private_key => "/opt/rt4/etc/keys/private_key.pem",
+	password => "test",
+	client_id => "96o6g1e6mot3j1ord2qq6ptvxcsbn4oh",
+	client_secret => "xhp3us3rX3kNLvMt9y3DcGSasqP6Orl3");
+
+my $id = $box->create_user($username);
+
+print $id, "\n";
diff --git a/scripts/delete_file.pl b/scripts/delete_file.pl
old mode 100644
new mode 100755
diff --git a/scripts/delete_user.pl b/scripts/delete_user.pl
old mode 100644
new mode 100755
diff --git a/scripts/delete_file.pl b/scripts/download_file.pl
old mode 100644
new mode 100755
similarity index 83%
copy from scripts/delete_file.pl
copy to scripts/download_file.pl
index c69c2c8..05cd9b9
--- a/scripts/delete_file.pl
+++ b/scripts/download_file.pl
@@ -2,8 +2,8 @@
 #
 use lib 'lib';
 use Data::Dumper;
-use Storage::Box::Auth;
 use Storage::Box::File;
+use Storage::Box::Auth;
 
 my ($user_id,$file_id) = @ARGV;
 
@@ -23,4 +23,7 @@ my $file = Storage::Box::File->new(
 	id => $file_id
 );
 
-$file->delete;
+open my $fh, "> $file_id" or die "couldnt write to file $!\n";
+print $fh $file->download;
+close $fh;
+
diff --git a/scripts/read_user.pl b/scripts/read_user.pl
old mode 100644
new mode 100755
diff --git a/scripts/update_user.pl b/scripts/update_user.pl
old mode 100644
new mode 100755
diff --git a/t/basic.t b/t/basic.t
index 0b54807..e220066 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -6,6 +6,5 @@ use lib 'lib';
 require_ok 'Storage::Box';
 require_ok 'Storage::Box::Auth';
 
-ok Storage::Box::Auth::generate_keys('test');
 
 done_testing;

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list