mirror of
https://gitlab.com/t6353/sharee.bike.git
synced 2024-11-15 23:26:34 +01:00
132 lines
2.7 KiB
Perl
132 lines
2.7 KiB
Perl
|
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;
|
||
|
|
||
|
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);
|
||
|
print "<--- get_bikes response_in:\n" . Dumper($response_in);
|
||
|
};
|
||
|
if ($@){
|
||
|
print "<--- failure get_bikes raw response_in:\n" . Dumper($ret_json) . "\n";
|
||
|
warn $@;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
#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;
|