This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclasses.clients.ps1
152 lines (120 loc) · 4.27 KB
/
classes.clients.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class SFxClient {
[string]$Realm = 'us1'
[string]$ApiVersion = 'v2'
[string]$Uri
[string]$Method
[string] hidden $Endpoint
[string] hidden $Path
[hashtable] hidden $Headers = @{ }
[hashtable] hidden $Body = @{ }
[string] hidden $EnvName_Realm = 'SFX_REALM'
[string] hidden $EnvName_AccessToken = 'SFX_ACCESS_TOKEN'
[string] hidden $EnvName_UserToken = 'SFX_USER_TOKEN'
SFxClient($endpoint, $path, $method) {
if ([Environment]::GetEnvironmentVariables().Contains($this.EnvName_Realm)) {
$this.SetRealm([Environment]::GetEnvironmentVariable($this.EnvName_Realm))
}
$this.Endpoint = $endpoint
$this.Path = $path
$this.Method = $method
$this.ConstructUri()
}
[void] ConstructUri() {
$this.Uri = 'https://{0}.{1}.signalfx.com/{2}/{3}' -f $this.Endpoint, $this.Realm, $this.ApiVersion, $this.Path
}
[SFxClient] SetRealm([string]$realm) {
$this.Realm = $realm
$this.ConstructUri()
return $this
}
[SFxClient] SetToken([string]$token) {
if ($this.Headers.ContainsKey('X-SF-TOKEN')) {
$this.Headers['X-SF-TOKEN'] = $token
}
else {
$this.Headers.Add('X-SF-TOKEN', $token)
}
return $this
}
[object] Invoke() {
$parameters = @{
Uri = $this.Uri
Headers = $this.Headers
ContentType = 'application/json'
Method = $this.Method
}
if ($this.Body.Count -gt 0) {
$parameters["Body"] = '[{0}]' -f ($this.Body | ConvertTo-Json)
}
return Invoke-RestMethod @parameters
}
[int64] GetTimeStamp() {
return [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
}
[int64] GetTimeStamp([DateTime]$timestamp) {
return [DateTimeOffset]::new($timestamp).ToUnixTimeMilliseconds()
}
}
class SFxClientApi : SFxClient {
$delimiter = '?'
SFxClientApi ($path, $method) : base ('api', $path, $method) {
if ([Environment]::GetEnvironmentVariables().Contains('SFX_USER_TOKEN')) {
$this.SetToken([Environment]::GetEnvironmentVariable('SFX_USER_TOKEN'))
}
}
[char] GetDelimiter() {
if ($this.delimiter -eq '?') {
$this.delimiter = '&'
return '?'
}
return $this.delimiter
}
[object] Invoke() {
$parameters = @{
Uri = $this.Uri
Headers = $this.Headers
ContentType = 'application/json'
Method = $this.Method
}
if ($this.Body.Count -gt 0) {
$parameters["Body"] = $this.Body | ConvertTo-Json
}
#try {
return Invoke-RestMethod @parameters# -ErrorAction Stop
#} catch {
# Throw ("StatusCode: {0}{1}StatusDescription: {2}" -f $_.Exception.Response.StatusCode.value__, [Environment]::NewLine, $_.Exception.Message )
# return $null
#}
}
}
class SFxClientIngest : SFxClient {
SFxClientIngest ($path, $method) : base ('ingest', $path, $method) {
if ([Environment]::GetEnvironmentVariables().Contains('SFX_ACCESS_TOKEN')) {
$this.SetToken([Environment]::GetEnvironmentVariable('SFX_ACCESS_TOKEN'))
}
}
}
class SFxClientBackfill : SFxClient {
[Text.StringBuilder] $Body
SFxClientBackfill () : base ('backfill', 'backfill', 'POST') {
$this.ApiVersion = 'v1'
# Apply the custom API version for this endpoint
$this.ConstructUri()
if ([Environment]::GetEnvironmentVariables().Contains('SFX_ACCESS_TOKEN')) {
$this.SetToken([Environment]::GetEnvironmentVariable('SFX_ACCESS_TOKEN'))
}
# At least 360 datapoints an hour in the JSON format SFx is expecting is at least 13,320 chars
# So, we might as well initialize the StringBuilder to hold at least that
$this.Body = [Text.StringBuilder]::new(13400)
}
[object] Invoke() {
$parameters = @{
Uri = $this.Uri
Headers = $this.Headers
ContentType = 'application/json'
Method = $this.Method
Body = $this.Body.ToString()
}
return Invoke-RestMethod @parameters
}
}