madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r22549: LJSUP-13176: JSON RPC code refactoring

Committer: sbelyaev
LJSUP-13176: JSON RPC code refactoring
U   trunk/cgi-bin/LJ/JSON/RPC/Item.pm
U   trunk/cgi-bin/LJ/JSON/RPC.pm
Modified: trunk/cgi-bin/LJ/JSON/RPC/Item.pm
===================================================================
--- trunk/cgi-bin/LJ/JSON/RPC/Item.pm	2012-08-02 09:18:10 UTC (rev 22548)
+++ trunk/cgi-bin/LJ/JSON/RPC/Item.pm	2012-08-02 10:56:00 UTC (rev 22549)
@@ -4,17 +4,14 @@
 use warnings;
 
 use LJ::API::Error;
-use LJ::API::Auth;
+use LJ::API::RpcAuth;
 
 #
 # json request and response jpc 2.0
 #
 
-use LJ::JSON;
-
 sub new {
     my ($class, $uri, $data) = @_;
-
     my $self = bless {}, $class;
 
     if ($data->{'fatal'}) {
@@ -25,40 +22,38 @@
     my $jsonrpc = $data->{'jsonrpc'};
     my $method  = $data->{'method'};
     my $params  = $data->{'params'};
+
     if (exists $data->{'id'}) {
         $self->{'id'} = $data->{'id'};
     }
 
-    $self->{'fatal'} = { 'error_code' => -32602, 'error_message' => 'Invalid params' } unless $params;
-    $self->{'fatal'} = { 'error_code' => -32602, 'error_message' => 'Invalid params' } if ref $params eq 'ARRAY';
-    if ($self->{'fatal'}) {
+    if (!$params || ref $params ne 'HASH') { 
+        $self->{'fatal'} = { 'error_code'    => -32602, 
+                             'error_message' => 'Invalid params' };
         return $self;
     }
 
-    my $access = LJ::API::Auth->rpc_access($uri, $params);
+    #
+    # Check for auth information
+    #
+    my $access = LJ::API::RpcAuth->rpc_access($uri, $params);
     $self->{'fatal'} = $access->{'error'};
     if ($self->{'fatal'}) {
         return $self;
     }
- 
-    $self->{'access_type'} = $access->{'type'};
 
-    $self->{'fatal'} = { 'error_code' => -32600, 'error_message' => 'Invalid Request' } unless $method;
-    $self->{'fatal'} = { 'error_code' => -32600, 'error_message' => 'Invalid Request' } if !$jsonrpc || $jsonrpc ne '2.0';
-
-    if ($self->{'fatal'}) {
-        return $self;
+    if (!$method || !$jsonrpc || $jsonrpc ne '2.0') { 
+        $self->{'fatal'} = { 'error_code'    => -32600, 
+                             'error_message' => 'Invalid Request' };
+        return $self;                             
     }
 
-
-    $self->{'uri'}    = $uri;
-    $self->{'method'} = $method;
-    $self->{'params'} = $params;
-    if (exists $data->{'id'}) {
-        $self->{'id'}     = $data->{'id'};
-    }
+    $self->{'uri'}         = $uri;
+    $self->{'method'}      = $method;
+    $self->{'params'}      = $params;
+    $self->{'access_type'} = $access->{'type'};
  
-    return $self,
+    return $self;
 }
 
 
@@ -70,7 +65,7 @@
 # - data
 #
 sub __construct_error_object {
-    LJ::API::Error->get_error 
+    LJ::API::Error->get_error;
 }
 
 sub response {
@@ -79,13 +74,15 @@
     my $result = $options->{'result'};
     my $error  = $options->{'error'};
     my $fatal  = $self->{'fatal'};
+    
     return if ($self->is_notitification && !$fatal);
     
     my $resp = { 'jsonrpc' => '2.0' };
             
     if ($result && $error) {
         # internal error
-        $fatal = { 'error_code' => -32603, 'error_message' => 'Internal error' };
+        $fatal = { 'error_code'    => -32603, 
+                   'error_message' => 'Internal error' };
     }
 
     ############################################################################
@@ -129,28 +126,18 @@
     #
     #############################################################################
     if ($error || $fatal) {
-        $error = $fatal if ($fatal);
-        my $error_information;
-
-        my $error_data; 
-        if ($error->{'defined'}) {
-            my $error_type     = $error->{'error_type'};
-            my $error_options  = $error->{'error_options'};
-
-            $error = LJ::API::Error->get_error($error_type, $error_options)->{'error'};
-        } 
-
-        my $error_code = $error->{'error_code'};
-        my $error_msg  = $error->{'error_message'};
-
-        $error_data->{'code'}   = $error_code;
-        $error_data->{'message'}= $error_msg; 
+        if ($fatal) {
+             $error = $fatal;
+        }
         
+        my %error_data = { 'code'    => $error->{'error_code'},
+                           'message' => $error->{'error_message'}, };
+       
         if ($error->{'data'}) {
-            $error_data->{'data'} = $error->{'data'};
+            $error_data{'data'} = $error->{'data'};
         }
 
-        $resp->{'error'} = $error_data;
+        $resp->{'error'} = \%error_data;
     }
 
     ##############################################################################

Modified: trunk/cgi-bin/LJ/JSON/RPC.pm
===================================================================
--- trunk/cgi-bin/LJ/JSON/RPC.pm	2012-08-02 09:18:10 UTC (rev 22548)
+++ trunk/cgi-bin/LJ/JSON/RPC.pm	2012-08-02 10:56:00 UTC (rev 22549)
@@ -15,25 +15,35 @@
 
     eval {
         my $data = LJ::JSON->from_json($request);
+        
+        $self->{'is_array'} = ref $data eq 'ARRAY';
 
-        if (ref $data eq 'ARRAY') {
+        if ($self->{'is_array'}) {
             foreach my $entry (@$data) {
-                push @{$self->{'items'}}, { 'item'  => LJ::JSON::RPC::Item->new($uri, $entry), };
+                my $item = LJ::JSON::RPC::Item->new($uri, $entry);
+                push @{$self->{'items'}}, { 'item' => $item, };
             }
 
             unless (@$data) {
-                my $fatal = { 'error_code' => -32600, 'error_message' => 'Invalid Request' };
-                $self->{'items'} = { 'item' => LJ::JSON::RPC::Item->new({ 'fatal' =>  $fatal })};
+                my $fatal = { 'error_code'    => -32600, 
+                              'error_message' => 'Invalid Request' };
+                              
+                my $item = LJ::JSON::RPC::Item->new( { 'fatal' =>  $fatal } );
+                $self->{'items'} = { 'item' => $item, };
             }
         } else {
-            $self->{'items'} = { 'item'   => LJ::JSON::RPC::Item->new($uri, $data),};
+            my $item = LJ::JSON::RPC::Item->new($uri, $data);
+            $self->{'items'} = { 'item' => $item, };
         }
     };
 
     if ($@) {
         warn $@ if $LJ::IS_DEV_SERVER;
-        my $fatal = { 'error_code' => -32700, 'error_message' => 'Parse error' };
-        $self->{'items'} = { 'item' => LJ::JSON::RPC::Item->new({ 'fatal' =>  $fatal })};
+        my $fatal = { 'error_code' => -32700, 
+                      'error_message' => 'Parse error' };
+
+        my $item = LJ::JSON::RPC::Item->new({ 'fatal' =>  $fatal });
+        $self->{'items'} = { 'item' => $item };
     }
 
     return $self;
@@ -42,30 +52,36 @@
 sub call {
     my ($self, $callback) = @_;
     my $items = $self->{'items'};
+    
+    if ($self->{'is_array'}){
+        $self->__call_array($items, $callback);
+    } else {
+        $self->__call_item($items, $callback);
+    }
+}
 
+sub __call_array {
+    my ($self, $items, $callback) = @_;
+
+    foreach my $entry (@$items) {
+        $self->__call_item($entry, $callback);
+    } 
+}
+
+sub __call_item {
+    my ($self, $entry, $callback) = @_;
+
     my $call_info = { 
         'source' => 'jsonrpc',
     };
 
-    if (ref $items eq 'ARRAY') {
-        foreach my $entry (@$items) {
-            my $item = $entry->{'item'};
-            next if $item->error;            
+    my $item   = $entry->{'item'};
+    return if $item->error;
 
-            my $method = $item->method;
-            my $params = $item->params;
+    my $method = $item->method;
+    my $params = $item->params;
 
-            $entry->{'result'} = $callback->($method, $params, $call_info);
-        }
-    } else {
-        my $item   = $items->{'item'};
-        return if $item->error;
-
-        my $method = $item->method;
-        my $params = $item->params;
-
-       $items->{'result'} = $callback->($method, $params, $call_info);
-    }
+    $entry->{'result'} = $callback->($method, $params, $call_info);
 }
 
 sub response {
@@ -73,10 +89,11 @@
     my $items = $self->{'items'};
     my $resp_data;
     
-    if (ref $items eq 'ARRAY') {
+    if ($self->{'is_array'}) {
         foreach my $entry (@$items) {
-            my $item   = $entry->{'item'};
+            my $item     = $entry->{'item'};
             my $response = $item->response($entry->{'result'});
+
             push @{$resp_data}, $response if $response;
         }
     } else {

Tags: livejournal, madeon, pm, sbelyaev
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments