Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allegro sandbox API added (API url needs to be changed later) #242

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions server/app/Console/Commands/allegroAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Console\Commands;

use App\Lib\AppStatusManager;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class allegroAPI extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'allegroAPI:GetAucSum {usrid} ';

/**
* The console command description.
*
* @var string
*/
protected $description = 'returns current sum of allegro auctions and stores it in cache';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$sum=0;

$auth_token = Cache::get('allegro_auth_token');
$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => 'https://api.allegro.pl.allegrosandbox.pl/offers/listing?sellingMode.format=%22BUY%20NOW%22&seller.login='.$this->argument('usrid'),
CURLOPT_HTTPHEADER => array('Accept: application/vnd.allegro.public.v1+json','Authorization: Bearer '.$auth_token),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
$info=curl_getinfo($ch);
if($info['http_code']==401){

$auth_token=$this->getAccessToken();
Cache::put('allegro_auth_token',$auth_token,43200);
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://api.allegro.pl.allegrosandbox.pl/offers/listing?sellingMode.format=%22BUY%20NOW%22&seller.login='.$this->argument('usrid'),
CURLOPT_HTTPHEADER => array('Accept: application/vnd.allegro.public.v1+json','Authorization: Bearer '.$auth_token),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);

}
$info=curl_getinfo($ch);
if($info['http_code']==200){
$var =json_decode($result,true);
foreach( $var['items'] as $key => $value ) {
foreach( $value as $key2 => $value2 ){
foreach( $value2 as $key3 => $value3 ){
$sum+=floatval($value2['sellingMode']['price']['amount']);
}
}

}
curl_close($ch);
Cache::put('allegro_auc_sum',$sum,3600);
}

$this->info($sum);

}
protected function getCurl($headers, $url, $content = null) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
));
if ($content !== null) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
return $ch;
}

protected function getAccessToken() {
$client_id=env('CLIENT_ID');
$client_secret=env('CLIENT_SECRET');
$authorization = base64_encode($client_id.':'. $client_secret);
$headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
$content = "grant_type=client_credentials";
$url = "https://allegro.pl.allegrosandbox.pl/auth/oauth/token";
$ch = $this->getCurl($headers, $url, $content);
$tokenResult = curl_exec($ch);
$resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($tokenResult === false || $resultCode !== 200) {
exit ("Something went wrong");
}
return json_decode($tokenResult)->access_token;
}
}
1 change: 1 addition & 0 deletions server/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function schedule(Schedule $schedule)
->everyFifteenMinutes();

$schedule->command('backup:run')->everyFifteenMinutes();
//$schedule->command('AllegroAPI:GetAucSum user')->everySecond();
}

/**
Expand Down
1 change: 0 additions & 1 deletion server/app/Http/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function __construct()
}

public function index(Request $request) {

$user = $request->user();

$data = [
Expand Down
1 change: 0 additions & 1 deletion server/app/Lib/AppStatusManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class AppStatusManager
*
* @var string
*/
public const MONEYBOX_VALUE = 'moneybox.value';

/**
* Reads a value stored with the given key in the database
Expand Down
Loading