Circle-Common

 view release on metacpan or  search on metacpan

lib/Circle/Common.pm  view on Meta::CPAN

package Circle::Common;

use 5.006;
use strict;
use warnings;
use Exporter 'import';
use URL::Encode qw(url_encode_utf8);
use LWP::UserAgent;
use HTTP::Request;
use Slurp;
use Try::Tiny;
use YAML;
use JSON;
use Carp;
use File::Share ':all';

our @EXPORT_OK = qw(
  load_config
  build_url_template
  http_json_post
  http_json_get
);

our $VERSION = '0.06';

my $config = undef;

sub load_config {
    if ($config) {
        return $config;
    }

    my $config_path;
    if (-e "./config.yml") {
        $config_path = "./config.yml";
    } else {
        $config_path = dist_file('Circle-Common', 'config.yml');
    }

    try {
        my $content = slurp($config_path);
        $config = Load($content);
    }
    catch {
        carp "cannot load config, error: $_";
    };
    return $config;
}

sub get_session_key {
    my $config       = load_config();
    my $user         = $config->{user};
    my $home         = $ENV{HOME};
    my $session_path = $user->{sessionPath};
    my $file_path    = "${home}/${session_path}";
    my $session_key  = '';
    # print "file_path: $file_path\n";
    try {
        my @lines        = slurp($file_path);
        my @session_keys = grep { chomp($_); $_ =~ /^sessionKey/; } @lines;
        if ( @session_keys > 0 ) {
            $session_key = $session_keys[0];
            $session_key =~ s/sessionKey=//;
        }
    }
    catch {
        carp "cannot read $session_path, error: $_";
    };

    return $session_key;
}



( run in 1.874 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )