Skip to content

Commit 9707994

Browse files
committed
Tags
1 parent 792f230 commit 9707994

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

Tests/Integration/Tags.Tests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Import-Module -Name Pester -Force
2+
Import-Module .\Toggl.API\Toggl.API.psm1 -Force
3+
4+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath "..\config.json"
5+
$config = Get-Content -Path $configPath | ConvertFrom-Json
6+
7+
$apiToken = $config.apiToken
8+
$workspaceId = $config.workspaceId
9+
10+
Describe 'Toggl Tag Integration Tests' {
11+
$tagName = "TestTagToRemove"
12+
13+
Context "New-TogglTag" {
14+
It "should create a new tag in the workspace" {
15+
$newTag = New-TogglTag -ApiToken $apiToken -WorkspaceId $workspaceId -Name $tagName
16+
$newTag | Should -Not -BeNullOrEmpty
17+
$newTag.name | Should -BeExactly $tagName
18+
$Script:tagId = $newTag.id
19+
}
20+
}
21+
22+
Context "Get-TogglTags" {
23+
It "should retrieve the list of tags for the workspace" -Skip:($Script:tagId -eq $null) {
24+
$tags = Get-TogglTags -ApiToken $apiToken -WorkspaceId $workspaceId
25+
$tags | Should -Not -BeNullOrEmpty
26+
$tags | Where-Object { $_.name -eq $tagName } | Should -Not -BeNullOrEmpty
27+
}
28+
}
29+
30+
Context "Remove-TogglTag" {
31+
It "should delete the tag from the workspace" -Skip:($Script:tagId -eq $null) {
32+
Remove-TogglTag -ApiToken $apiToken -WorkspaceId $workspaceId -TagId $Script:tagId
33+
34+
# Verify the tag was deleted
35+
$tags = Get-TogglTags -ApiToken $apiToken -WorkspaceId $workspaceId
36+
$tags | Where-Object { $_.id -eq $Script:tagId } | Should BeNullOrEmpty
37+
}
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<#
2+
.SYNOPSIS
3+
Lists workspace tags from Toggl.
4+
5+
.DESCRIPTION
6+
This cmdlet retrieves the list of tags for a specified workspace from Toggl. It sends a GET request to the Toggl API.
7+
8+
.PARAMETER ApiToken
9+
The API token for authentication. (Mandatory)
10+
11+
.PARAMETER WorkspaceId
12+
Numeric ID of the workspace. (Mandatory)
13+
14+
.EXAMPLE
15+
Get-TogglTags -ApiToken "your_api_token" -WorkspaceId 123456
16+
#>
17+
function Get-TogglTags {
18+
[CmdletBinding()]
19+
param (
20+
[Parameter(Mandatory = $true)]
21+
[string]$ApiToken,
22+
23+
[Parameter(Mandatory = $true)]
24+
[int]$WorkspaceId
25+
)
26+
27+
$url = "$Global:TogglBaseUrl/workspaces/$WorkspaceId/tags"
28+
29+
$headers = Get-TogglAuthHeader -ApiToken $ApiToken
30+
31+
try {
32+
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
33+
return $response
34+
}
35+
catch {
36+
Write-Error "Failed to retrieve tags: $_"
37+
}
38+
}
39+
40+
Export-ModuleMember -Function Get-TogglTags
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<#
2+
.SYNOPSIS
3+
Creates a new tag in a specified workspace in Toggl.
4+
5+
.DESCRIPTION
6+
This cmdlet creates a new tag in a specified workspace in Toggl. It sends a POST request to the Toggl API.
7+
8+
.PARAMETER ApiToken
9+
The API token for authentication. (Mandatory)
10+
11+
.PARAMETER WorkspaceId
12+
Numeric ID of the workspace. (Mandatory)
13+
14+
.PARAMETER Name
15+
The name of the tag. (Mandatory)
16+
17+
.EXAMPLE
18+
New-TogglTag -ApiToken "your_api_token" -WorkspaceId 123456 -Name "NewTag"
19+
#>
20+
function New-TogglTag {
21+
[CmdletBinding()]
22+
param (
23+
[Parameter(Mandatory = $true)]
24+
[string]$ApiToken,
25+
26+
[Parameter(Mandatory = $true)]
27+
[int]$WorkspaceId,
28+
29+
[Parameter(Mandatory = $true)]
30+
[string]$Name
31+
)
32+
33+
$url = "$Global:TogglBaseUrl/workspaces/$WorkspaceId/tags"
34+
35+
$body = @{
36+
name = $Name
37+
}
38+
39+
$jsonBody = $body | ConvertTo-Json -Compress -Depth 10
40+
41+
$headers = Get-TogglAuthHeader -ApiToken $ApiToken
42+
$headers.Add("Content-Type", "application/json")
43+
44+
try {
45+
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $jsonBody
46+
return $response
47+
}
48+
catch {
49+
Write-Error "Failed to create tag: $_"
50+
}
51+
}
52+
53+
Export-ModuleMember -Function New-TogglTag
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<#
2+
.SYNOPSIS
3+
Deletes a tag in a specified workspace in Toggl.
4+
5+
.DESCRIPTION
6+
This cmdlet deletes a tag in a specified workspace in Toggl. It sends a DELETE request to the Toggl API.
7+
8+
.PARAMETER ApiToken
9+
The API token for authentication. (Mandatory)
10+
11+
.PARAMETER WorkspaceId
12+
Numeric ID of the workspace. (Mandatory)
13+
14+
.PARAMETER TagId
15+
Numeric ID of the tag. (Mandatory)
16+
17+
.EXAMPLE
18+
Remove-TogglTag -ApiToken "your_api_token" -WorkspaceId 123456 -TagId 789012
19+
#>
20+
function Remove-TogglTag {
21+
[CmdletBinding()]
22+
param (
23+
[Parameter(Mandatory = $true)]
24+
[string]$ApiToken,
25+
26+
[Parameter(Mandatory = $true)]
27+
[int]$WorkspaceId,
28+
29+
[Parameter(Mandatory = $true)]
30+
[int]$TagId
31+
)
32+
33+
$url = "$Global:TogglBaseUrl/workspaces/$WorkspaceId/tags/$TagId"
34+
35+
$headers = Get-TogglAuthHeader -ApiToken $ApiToken
36+
37+
try {
38+
Invoke-RestMethod -Uri $url -Method Delete -Headers $headers
39+
Write-Output "Tag deleted successfully."
40+
}
41+
catch {
42+
Write-Error "Failed to delete tag: $_"
43+
}
44+
}
45+
46+
Export-ModuleMember -Function Remove-TogglTag

0 commit comments

Comments
 (0)