[pmg-devel] [PATCH pmg-api v2 2/5] api: statistics: refactor return for detail calls

Stoiko Ivanov s.ivanov at proxmox.com
Thu Jan 21 16:51:03 CET 2021


all api methods returning information for a particular sender,
receiver or contact have similar returns.

This commit pulls the common ones out into a sub like the common method
parameters in $default_properties.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
v1->v2:
* keep the common properties in a sub instead of a hash (for consistency
  with $default_properties)
 src/PMG/API2/Statistics.pm | 109 ++++++++++++++-----------------------
 1 file changed, 40 insertions(+), 69 deletions(-)

diff --git a/src/PMG/API2/Statistics.pm b/src/PMG/API2/Statistics.pm
index adc7650..064865d 100644
--- a/src/PMG/API2/Statistics.pm
+++ b/src/PMG/API2/Statistics.pm
@@ -257,6 +257,40 @@ __PACKAGE__->register_method ({
 	return $res;
     }});
 
+my $detail_return_properties = sub {
+    my ($prop) = @_;
+
+    $prop //= {};
+
+    $prop->{time} = {
+	description => "Receive time stamp",
+	type => 'integer',
+    };
+
+    $prop->{bytes} = {
+	description => "Mail traffic (Bytes).",
+	type => 'number',
+    };
+
+    $prop->{blocked} = {
+	description => "Mail was blocked.",
+	type => 'boolean',
+    };
+
+    $prop->{spamlevel} = {
+	description => "Spam score.",
+	type => 'number',
+    };
+
+    $prop->{virusinfo} = {
+	description => "Virus name.",
+	type => 'string',
+	optional => 1,
+    };
+
+    return $prop;
+};
+
 __PACKAGE__->register_method ({
     name => 'contactdetails',
     path => 'contact/{contact}',
@@ -282,33 +316,12 @@ __PACKAGE__->register_method ({
 	type => 'array',
 	items => {
 	    type => "object",
-	    properties => {
-		time => {
-		    description => "Receive time stamp",
-		    type => 'integer',
-		},
+	    properties => $detail_return_properties->({
 		sender => {
 		    description => "Sender email.",
 		    type => 'string',
 		},
-		bytes => {
-		    description => "Mail traffic (Bytes).",
-		    type => 'number',
-		},
-		blocked => {
-		    description => "Mail was blocked.",
-		    type => 'boolean',
-		},
-		spamlevel => {
-		    description => "Spam score.",
-		    type => 'number',
-		},
-		virusinfo => {
-		    description => "Virus name.",
-		    type => 'string',
-		    optional => 1,
-		},
-	    },
+	    }),
 	},
     },
     code => sub {
@@ -421,33 +434,12 @@ __PACKAGE__->register_method ({
 	type => 'array',
 	items => {
 	    type => "object",
-	    properties => {
-		time => {
-		    description => "Receive time stamp",
-		    type => 'integer',
-		},
+	    properties => $detail_return_properties->({
 		receiver => {
 		    description => "Receiver email.",
 		    type => 'string',
 		},
-		bytes => {
-		    description => "Mail traffic (Bytes).",
-		    type => 'number',
-		},
-		blocked => {
-		    description => "Mail was blocked.",
-		    type => 'boolean',
-		},
-		spamlevel => {
-		    description => "Spam score.",
-		    type => 'number',
-		},
-		virusinfo => {
-		    description => "Virus name.",
-		    type => 'string',
-		    optional => 1,
-		},
-	    },
+	    }),
 	},
     },
     code => sub {
@@ -568,33 +560,12 @@ __PACKAGE__->register_method ({
 	type => 'array',
 	items => {
 	    type => "object",
-	    properties => {
-		time => {
-		    description => "Receive time stamp",
-		    type => 'integer',
-		},
+	    properties => $detail_return_properties->({
 		sender => {
 		    description => "Sender email.",
 		    type => 'string',
 		},
-		bytes => {
-		    description => "Mail traffic (Bytes).",
-		    type => 'number',
-		},
-		blocked => {
-		    description => "Mail was blocked.",
-		    type => 'boolean',
-		},
-		spamlevel => {
-		    description => "Spam score.",
-		    type => 'number',
-		},
-		virusinfo => {
-		    description => "Virus name.",
-		    type => 'string',
-		    optional => 1,
-		},
-	    },
+	    }),
 	},
     },
     code => sub {
-- 
2.20.1





More information about the pmg-devel mailing list