Net-Respite/lib/Net/Respite/Base.pod
=head1 NAME
Net::Respite::Base - base class for Respite related modules that can be used from a server or commandline
=head1 SYNOPSIS
package Foo;
use base qw(Net::Respite::Base);
my $meta = {lib_dirs => 1, dispatch_type => 'cache'};
sub api_meta { $meta }
package Foo::Bar;
sub somecall__meta { {desc => 'A method'} }
sub somecall {
my ($self, $args) = @_;
return {foo => 1};
}
my $f = Foo->new;
my $data = $f->bar_somecall;
#-----------------#
package Foo;
use strict;
use warnings;
use base qw(Net::Respite::Base);
# sub api_meta { {} } # optional configuration
sub api_meta {
return shift->{'api_meta'} ||= { # vtable cached here
methods => {
foo => 'bar', # alias a method
baz => sub { }, # custom
},
namespaces => {
foo_child => 1,
bar_child => 1,
},
lib_dirs => {
$dir => 1, # load all .pm files as namespaces
},
};
}
sub foo__meta { {} }
sub foo {
my ($self, $args) = @_;
$self->validate_args($args);
return {...};
}
my $obj = Foo->new;
$obj->run_method("foo", $args);
# will do logging and utf8 munging - used by Net::Respite::Server or Net::Respite::CommandLine
$obj->foo($args); # no logging or utf8 munging
###----------------------------------------------------------------###
use Net::Respite::Base;
my $obj = Net::Respite::Base->new({
api_meta => {
methods => {
foo => 'bar',
},
namespaces => {
Foo => {},
},
},
});
=head1 Respite_META
The module can specify a api_meta override, or a api_meta hashref
can be passed to Net::Respite::Base->new. The following keys are honored
from api_meta.
=over 4
=item methods
Can be a hard coded list of supported methods.
methods => {
foo => 'bar',
fii => sub { return {data => 'some data'} },
},
=item namespaces
Hard coded list of method namespaces that will be used to map methods
to packages spaces.
namespaces => {
customer => '__',
package => '__',
},
A module name will be generated from the namespace. The the methods
reside in a different location, it is possible to pass along the package.
namespaces => {
customer => {
match => '__',
package => 'SomeSpace::Customer',
},
},
Methods are looked for in this namespace package space. Method names
used in the api must either begin with a __ (__foo_method), and/or
they must have a corresponding __meta entry (foo_method__meta). This
allows for non-Respite methods to remain non-Respite easily. Additionally you
can use the restrict method call to narrow this down even farther.
=item lib_dirs
Dynamic directory of method namespaces. Items in lib_dirs will be used
for a path search that will populate a namespace entry.
lib_dirs => {
"$config::config{'rootdir_server'}/api_lib" => 1,
},
Depending upon the path chosen, it may be necessary to supply a pkg_prefix.
lib_dirs => {
"$config::config{'rootdir_server'}/lib/YAR" => {
pkg_prefix => 'YAR',
},
},
Alternately, it is possible to set lib_dirs equal to "1" which will
make it automatically follow the previous behavior. Note though that
additional looked up modules must be found relative to the location of
the parent module (@INC is not used).
=item utf8_encoded
Default false. When false, data passed should be properly utf8
decoded (or have no utf8 data at all). When true, data passed is
assumed to be utf8 encoded meaning that it will need to be decoded
before calling json->encode.
Additionally, the true value can be a hashref of methods that need
this treatment. This is useful if you know some of your methods have
utf8 data, while others do not.
(Note: Conversely when the non-json transport is finalized, it will
need to call decode_utf8 to make sure data is ready for the
transport.)
=item dispatch_type
Can be one of new, cache, or morph. Default is new. When "new"
is selected, a new object will be created with each dispatch call and
it will contain a reference to the parent in the key named "base". When
"cache" is selected, a cached object will be used - the object is cached
inside of the base object. When "morph is
selected, rather than creating a new object, the base object is temporarily
blessed into the new object class.
=item enforce_requires_admin
Default false. If true, then validate_args will honor the requires_admin
by calling require_admin if it appears in the __meta for the method.
=item allow_nested
Allow for nested package inheritance. The nested namespace package
must provide its own api_meta.
=back
=head1 METHOD NAME RESOLUTION
TODO - document how we go from an Respite method name to its
corresponding location.
Talk about builtins, methods overrides, namespaces, lib_dirs, and
the __ prefix and __meta suffix.
=head1 METHODS
This is an outdated list from the _Respite.pm import.
=over 4
=item base
When dispatch_type is set to C<new> or C<cache>, this method will
provide access to the parent dispatching to the sub namespace.
=item base_class
If a child namespace is used directly, base_class will be looked
at when the C<base> method is called to create a parent base object.
=item validate_args
See L<Net::Respite::Validate>
=item verify_admin
Uses Net::Respite::Client to call the emp_auth service and verify a passed
in token.
=item api_preload
Called from Net::Respite::Server when a server is being started. This
method can be used to pre load all necessary modules to avoid
a penalty later on. Note that any overrides should likely
call ->SUPER::api_preload as well.
=back
The following methods are normally set when called from Net::Respite::Server
or Net::Respite::CommandLine. If Net::Respite::Base is used outside of these
mediums, then the corresponding $self->{'propertyname'} values must be set
during initialization in order to use these methods. If namespaces
are used, the child class will typically fail back and look at the
->base->method value if necessary.
=over 4
=item remote_ip
The IP on the client machine calling this service. This is advisory.
Net::Respite::Client will use cmdline for this value when called through
Net::Respite::CommandLine on the remote box.
=item remote_user
The user on the client machine calling this service. This is advisory.
=item transport
Defaults to ''. From Net::Respite::Server it defaults to C<json> (Respite),
C<form> (Post variables), and C<form-doc> (autodoc interface). From
Net::Respite::CommandLine it defaults to C<cmdline>. It is normal in web
interfaces that this value should be set to C<gui>.
=item api_ip
The IP used to talk to the service when used as an Respite server. It will
be cmdline when called from Net::Respite::CommandLine.
=item api_brand
The brand used during api communication. As a special case, if C<is_local>
is true, the $ENV{'PROV'} value can be used to specify the brand.
=item admin_user
The authenticated administrative username. Will only be set if
require_admin has been called. Dies is not set.
=item is_server
Set by Net::Respite::CommandLine and Net::Respite::Server.
=item is_local
Only true if transport is cmdline or gui. Essentially this is a "non-api"
check.
=item is_authed
True if admin_user is set. False if not (does not die).
=item employee
Will return an Employee object based on admin_user.
=back
=head1 RESPONSE
Responses from called methods should typically be hashrefs of
data. These responses will be encoded by the appropriate system.
Typically the encoding will be json (during Net::Respite::Server), but possibly
other forms (Perl, JSON, YAML, CSV) such as during Net::Respite::CommandLine.
Methods can also return psgi style responses though this is typically more
rare. (It allows for other encodings or page displays).
When a hashref is returned the following keys may also be returned to
give additional information to the transport layer:
=over 4
=item _utf8_encoded
This flag signals that the data is already utf8 encoded meaning that if
a JSON transport layer is used, the data will need to be first decoded before
being passed to JSON->encode to avoid double encoding. Typically, this
should be done by setting utf8_encoded in api_meta - though the _utf8_encoded
flag allows for one-off operations.
=item _extra_headers
This should be an arrayref of arrayrefs containing key/val pairs. When used
under Net::Respite::Server, these will be sent as additional http headers. When using
Net::Respite::Client, the headers can be seen in the 'headers' property of a response
object (non-flat).
sub __some_method {
return {
_extra_headers => [
['Set-Cookie' => 'foo=bar'],
['X-Some-Header' => 'someval'],
],
normal_data_key => 'val',
};
}
=back
=cut