For now I have success on integrating API in my web application, by taking portion of the data retrieve from the API such as steamid I can make a get request using guzzle to get other data such as game stats to display on my user profile.
By rights in my web application, a user can access the /profile routes to edit or connect to their steam account to get other data such as game stats to be display on their profile https://i.imgur.com/CWYL8xB.png . Example screenshot of user that have already integrate their account https://i.imgur.com/KViazYi.png . Example of error when going to the /profile routes https://i.imgur.com/Ob0rOgN.png
However, if a registered new user which havent connect their steam account in order to get the other data such as game stats. An error is display from the guzzle function. Is there any approach I can take to make a specific condition like if a user havent connect their steam account, dont do the get request from guzzle function? Or other nice approach that I can follow, is appreciated.
(Steam controller)
public function handleProviderCallback($provider, Steam $steam)
{
$data = Socialite::driver($provider)->user();
$steam = new Steam();
$steam->user_id = auth()->user()->id;
$steam->profile_id = auth()->user()->profile->id;
$steam->steamid = $data->user['steamid'];
$steam->profileurl = $data->user['profileurl'];
$steam->save();
return redirect('/home')->with('success', 'Steam Connected');
}
(Steam model)
public static function steamProfile()
{
$client = new \GuzzleHttp\Client(['verify' => false]);
$response = $client->request('GET',
'https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/', [
'query' => [
'appid'=>'730',
'key' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'steamid' => auth()->user()->steam['steamid']
],
]);
$data = json_decode($response->getBody(), true);
return $data;
}
(Profile Controller @index)
public function index(Steam $steam)
{
$data = Steam::steamProfile();
return view ('profiles.index', [
'data' => $data,
'profile' => Auth::user()->profile,
'steam' => Auth::user()->$steam
]);
}
(Profile index.blade.php)
<div class="card-header">
Profile Dashboard
<div class="float-right">
@if(is_null(auth()->user()->steam))
<a href="/login/steam" class="btn btn-outline-dark btn-sm">
{{ __('Connect Steam') }}
</a>
<a href="/profile/{{$profile->id}}/edit" class="btn btn-outline-dark btn-sm">
{{ __('Edit Profile') }}
</a>
@else
<a href="/profile/{{$profile->id}}/edit" class="btn btn-outline-dark btn-sm">
{{ __('Edit Profile') }}
</a>
@endif
</div>
</div>
<div class="container">
<div class="row">
<div class="col-3 pl-5">
<br/>
@if(is_null($profile->image))
<img src="/jpg/default.jpg" class="img-thumbnail" width="208" height="208">
@else
<img src="storage/profiles_image/{{$profile->image}}" class="img-thumbnail" width="208"
height="208">
@endif
</div>
<div class="col-9">
<br/>
<b>{{$profile->username ?? 'No Information'}}</b><br/>
<br/>
<div class="h7 text-muted">{{$profile->description ?? 'No Information'}}</div>
<br/>
<b>{{$profile->city ?? 'City'}}</b>, <b>{{$profile->state ?? 'State'}}</b> <br/>
<br/>
<b>Following:</b> 0 <b>Followers:</b> 0
</div>
</div>
</div>
<div class="container pt-4">
<table class="table table-bordered">
<tbody>
<tr>
<th class="card-header" colspan="2"></th>
</tr>
<tr>
<th scope="row" width="50%">steam_id</th>
<td>{{$profile->steam->steamid ?? 'No Information'}}</td>
</tr>
<tr>
<th scope="row">steam_url</th>
<td>{{$profile->steam->profileurl ?? 'No Information'}}</td>
</tr>
</tbody>
</table>
</div>
<div class="container pt-2">
<table class="table table-bordered">
<tbody>
<tr>
<th class="card-header" colspan="2"></th>
</tr>
@foreach($data as $stats)
@foreach($stats['stats'] as $stat)
<tr>
<th scope="row" width="50%">{{$stat['name'] ?? 'No Information'}}</th>
<td>{{$stat['value'] ?? 'No Information'}}</td>
</tr>
@endforeach
@endforeach
</tbody>
</table>
</div>