sharee.bike/copri4/main/src/Mod/APIsigoclient.pm

136 lines
2.8 KiB
Perl
Raw Normal View History

2022-03-26 10:19:13 +01:00
package APIsigoclient;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#Client for sigo
#
#use lib qw(/var/www/copri-bike/shareeapp-fr01/src);
use warnings;
use strict;
use POSIX;
use Exporter;
our @ISA = qw (Exporter);
use POSIX;
use CGI;
use JSON;
use LWP::UserAgent;
use DateTime;
use Time::Piece;
use Scalar::Util qw(looks_like_number);
use Config::General;
use Lib::Config;
use Mod::DBtank;
use Mod::Basework;
use Data::Dumper;
my $cf = new Config;
my $dbt = new DBtank;
my $bw = new Basework;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $api_file = "/var/www/copri4/shareeconf/apikeys.cfg";
my $aconf = Config::General->new($api_file);
my %apikeyconf = $aconf->getall;
my $ua = LWP::UserAgent->new;
$ua->agent("sharee sigo-client");
my $size = $ua->max_size;
my $bytes = 1000;
$ua->max_size( $bytes );
$ua->default_header( 'x-api-key' => $apikeyconf{sigo}->{api_key} );
my $json = JSON->new->allow_nonref;
my %varenv = $cf->envonline();
my $response_in = {};
my $dbh = "";
my $owner = 169;
open(FILE,">>$varenv{logdir}/APIsigo_client.log");
print FILE "\n*** $now_dt 'sigo client' \n";
sub get_bikes {
my $self = shift;
2022-03-30 13:06:39 +02:00
open(FILE,">>$varenv{logdir}/APIsigo_client.log");
print FILE "\n*** $now_dt 'sigo client' \n";
2022-03-26 10:19:13 +01:00
my $endpoint = "https://sigo.dev.sigo.green/api/v1/bikes";
print "$endpoint\n";
my $rest_json = "";
my $ret_json = get_sigo("","$endpoint",$rest_json);
eval {
$response_in = decode_json($ret_json);
2022-03-30 13:06:39 +02:00
print FILE "<--- get_bikes response_in:\n" . Dumper($response_in);
2022-03-26 10:19:13 +01:00
};
if ($@){
2022-03-30 13:06:39 +02:00
print FILE "<--- failure get_bikes raw response_in:\n" . Dumper($ret_json) . "\n";
2022-03-26 10:19:13 +01:00
warn $@;
}
2022-03-30 13:06:39 +02:00
close(FILE);
2022-03-26 10:19:13 +01:00
}
#main GET
sub get_sigo {
my $self = shift;
my $endpoint = shift || "";
my $rest_json = shift || "";
my $sigo_request = "$endpoint";
print FILE "===> GET sigo >> " . $sigo_request . "\n" . $rest_json . "\n";
my $req = HTTP::Request->new(GET => "$sigo_request");
$req->content_type('application/json');
$req->content($rest_json);
my $res = $ua->request($req);
if ($res->is_success) {
#print $res->content;
return $res->content;
print $res->status_line, "\n";
}else {
print $res->status_line, "\n";
}
}
sub post_sigo {
my $self = shift;
my $endpoint = shift || "";
my $rest_json = shift || "";
my $sigo_request = "$endpoint";
print FILE "===> POST sigo >> " . $sigo_request . "\n" . $rest_json . "\n";
my $req = HTTP::Request->new(POST => "$sigo_request");
$req->content_type('application/json');
$req->content($rest_json);
my $res = $ua->request($req);
if ($res->is_success) {
#print $res->content;
return $res->content;
print $res->status_line, "\n";
}else {
print $res->status_line, "\n";
}
}
close(FILE);
1;