Skip to content

Commit

Permalink
Rework Get-SteamFriendList (#75)
Browse files Browse the repository at this point in the history
* refactor cmdlet + test cases

* typo

* Mock Get-SteamAPIKey

* test case for private profile

* remove verbose output

* add tag

* single quotes

* comment based help
  • Loading branch information
hjorslev authored Apr 19, 2024
1 parent 7806f93 commit 8c0a264
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
63 changes: 35 additions & 28 deletions SteamPS/Public/API/Get-SteamFriendList.ps1
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
function Get-SteamFriendList {
<#
.SYNOPSIS
Returns the friend list of any Steam user.
Fetches the friend list of tje specified Steam user.
.DESCRIPTION
Retrieves the friend list of a Steam user whose profile visibility is set to "Public".
This cmdlet fetches the friend list of a Steam user using the provided SteamID64. It retrieves data only from public profiles.
.PARAMETER SteamID64
Specifies the 64-bit Steam ID of the user whose friend list will be retrieved.
The 64-bit Steam ID of the user whose friend list is to be fetched.
.PARAMETER Relationship
Specifies the relationship type to filter the friend list. Possible values are 'all' or 'friend'. Default is 'friend'.
.PARAMETER OutputFormat
Specifies the format of the output. Options are 'json' (default), 'xml', or 'vdf'.
The relationship type used to filter the friend list. The possible values are 'all' or 'friend'. The default is 'friend'.
.EXAMPLE
Get-SteamFriendList -SteamID64 76561197960435530
Retrieves the friend list of the specified user.
.EXAMPLE
Get-SteamFriendList -SteamID64 76561197960435530 -OutputFormat xml
Retrieves the friend list of the specified user and outputs it in XML format.
This example fetches the friend list of the user with the specified SteamID64.
.INPUTS
System.Int64
.OUTPUTS
Returns a string formatted as JSON, XML, or VDF representing the user's friend list.
The friend list contains the following properties:
- steamid: 64-bit Steam ID of the friend.
- relationship: Relationship qualifier.
- friend_since: Unix timestamp of when the relationship was established.
Outputs a string formatted as JSON, XML, or VDF representing the user's friend list.
The friend list includes the following properties:
- steamid: The friend's 64-bit Steam ID.
- relationship: The qualifier of the relationship.
- friend_since: The Unix timestamp indicating when the relationship was established.
.NOTES
Author: Frederik Hjorslev Nylander
Expand All @@ -52,22 +43,38 @@
[Parameter(Mandatory = $false,
HelpMessage = 'Specifies the relationship type to filter the friend list. Possible values are "all" or "friend". Default is "friend".')]
[ValidateSet('all', 'friend')]
[string]$Relationship = 'friend',

[Parameter(Mandatory = $false,
HelpMessage = 'Specifies the format of the output. Options are "json" (default), "xml", or "vdf".')]
[ValidateSet('json', 'xml', 'vdf')]
[string]$OutputFormat = 'json'
[string]$Relationship = 'friend'
)

begin {
Write-Verbose -Message "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
}

process {
$Request = Invoke-WebRequest -Uri "https://api.steampowered.com/ISteamUser/GetFriendList/v1/?key=$(Get-SteamAPIKey)&steamid=$SteamID64&relationship=$Relationship&format=$OutputFormat" -UseBasicParsing

Write-Output -InputObject $Request.Content
$Request = Invoke-RestMethod -Uri 'https://api.steampowered.com/ISteamUser/GetFriendList/v1/' -UseBasicParsing -ErrorAction SilentlyContinue -Body @{
key = Get-SteamAPIKey
steamid = $SteamID64
relationship = $Relationship
}

if ($Request) {
foreach ($Item in $Request.friendslist.friends) {
[PSCustomObject]@{
SteamID64 = [int64]$Item.steamid
Relationship = $Item.relationship
FriendSince = ((Get-Date "01.01.1970") + ([System.TimeSpan]::FromSeconds($Item.friend_since))).ToString("yyyy-MM-dd HH:mm:ss")
}
}
} elseif ($null -eq $Request) {
$Exception = [Exception]::new("No friend list found for $SteamID64. This might be because the profile is private.")
$ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
$Exception,
'NoFriendsListFound',
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Request
)
$PSCmdlet.WriteError($ErrorRecord)
}
} # Process

end {
Expand Down
48 changes: 48 additions & 0 deletions Tests/Unit/Public/Get-SteamFriendList.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Describe 'Get-SteamFriendList Tests' -Tag 'Unit' {
BeforeAll {
. $SteamPSModulePath\Private\API\Get-SteamAPIKey.ps1
Mock -CommandName Get-SteamAPIKey -ModuleName SteamPS -MockWith {
return $true
}
}

Context 'When retrieving friend list' {
BeforeAll {
Mock -CommandName Invoke-RestMethod -ModuleName SteamPS -MockWith {
return '{ "friendslist": { "friends":[ { "steamid":"11111197987580876", "relationship":"friend", "friend_since":1454562868 }, { "steamid":"99991197997032827", "relationship":"friend", "friend_since":1614836906 }] } }' | ConvertFrom-Json
} # Mock
} # Context

It 'Retrieves friend list correctly' {
$FriendList = Get-SteamFriendList -SteamID64 123456789

# Check if the FriendList is not null
$FriendList | Should -Not -BeNullOrEmpty

$FriendList[0].SteamID64 | Should -Be '11111197987580876'
$FriendList[0].Relationship | Should -Be 'friend'
$FriendList[0].FriendSince | Should -Be '2016-02-04 05:14:28'

$FriendList[1].SteamID64 | Should -Be '99991197997032827'
$FriendList[1].Relationship | Should -Be 'friend'
$FriendList[1].FriendSince | Should -Be '2021-03-04 05:48:26'
}

Context 'With invalid SteamID64' {
It 'Throws an error' {
{ Get-SteamFriendList -SteamID64 'invalidID' } | Should -Throw
}
}
}

Context 'With a private profile' {
BeforeAll {
Mock -CommandName Invoke-RestMethod -ModuleName SteamPS -MockWith {
return $null
} # Mock
}
It 'Writes an error' {
{ Get-SteamFriendList -SteamID64 1234567890 -ErrorAction Stop } | Should -Throw
}
}
}

0 comments on commit 8c0a264

Please sign in to comment.