Private/Get-DeeplApiUri.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 26811398-0439-4815-b931-4c1673a0373c
 
.AUTHOR diko@admins-little-helper.de
 
.COMPANYNAME
 
.COPYRIGHT (c) 2022 All rights reserved.
 
.TAGS DeepL Translate Translation
 
.LICENSEURI https://github.com/admins-little-helper/DeeplTranslate/blob/main/LICENSE
 
.PROJECTURI https://github.com/admins-little-helper/DeeplTranslate
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
    1.0.0
    Initial release
 
#>



<#
 
.DESCRIPTION
    Contains a function to retrieve the DeepL API Uri based on a given ApiKey value.
 
.LINK
    https://github.com/admins-little-helper/DeeplTranslate
 
#>



function Get-DeeplApiUri {
    <#
    .SYNOPSIS
        Retrieves the DeepL API Uri based on a given ApiKey value.
 
    .DESCRIPTION
        Retrieves the DeepL API Uri based on a given ApiKey value.
 
    .PARAMETER ApiKey
        DeepL Api Key string value.
 
    .EXAMPLE
        Get-DeeplApiUri ApiKey "3363e9e1-00d8-45a1-9c0c-b93ee03f8c13"
 
        This example will return the DeepL Pro service URI because the ApiKey does not end with ':fx'.
 
    .EXAMPLE
        Get-DeeplApiUri ApiKey "3363e9e1-00d8-45a1-9c0c-b93ee03f8c13:fx"
 
        This example will return the DeepL Free service URI because the ApiKey ends with ':fx'.
 
    .INPUTS
        System.String
 
    .OUTPUTS
        System.String
 
    .NOTES
        Author: Dieter Koch
        Email: diko@admins-little-helper.de
 
    .LINK
        https://github.com/admins-little-helper/DeeplTranslate/blob/main/Help/Get-DeeplApiUri.txt
 
    #>


    [Cmdletbinding()]
    [OutputType([string])]
    param (
        [Parameter(Mandatory, ValueFromPipeline)]
        [string[]]
        $ApiKey
    )

    process {
        foreach ($ApiKeyElement in $ApiKey) {
            # Set the base URI to either the DeepL API Pro or DeepL API Free service depending on the ApiKey value specified.
            # DeepL API Free authentication keys can be identified easily by the suffix ":fx" (e.g., 279a2e9d-83b3-c416-7e2d-f721593e42a0:fx).
            # For more information refer to https://www.deepl.com/docs-api/api-access/authentication/.
            $BaseUri = if ($ApiKeyElement -match "(:fx)$") {
                Write-Verbose -Message "The ApiKey specified ends with ':fx'. Using DeepL Api Free service URI."
                'https://api-free.deepl.com/v2'
            }
            else {
                Write-Verbose -Message "The ApiKey specified does not end with ':fx'. Using DeepL Api Pro service URI."
                'https://api.deepl.com/v2'
            }

            $BaseUri
        }
    }
}


#region EndOfScript
<#
################################################################################
################################################################################
#
# ______ _ __ _____ _ _
# | ____| | | / _| / ____| (_) | |
# | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_
# | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __|
# | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_
# |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__|
# | |
# |_|
################################################################################
################################################################################
# created with help of http://patorjk.com/software/taag/
#>

#endregion