-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientWrapper.ps1
More file actions
90 lines (71 loc) · 3.33 KB
/
Copy pathHttpClientWrapper.ps1
File metadata and controls
90 lines (71 loc) · 3.33 KB
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
class HttpClientWrapper {
hidden [System.Net.Http.HttpClient]$client
hidden [System.Net.Http.HttpClientHandler]$handler
hidden [string]$contentType = "application/json"
# default ctor
HttpClientWrapper() {
$this.SetOptions()
}
# param ctor, use single url for all requests
HttpClientWrapper([string]$url) {
$this.SetOptions()
$this.client.BaseAddress = $url
}
# param ctor, use single url + defines content type for all requests
HttpClientWrapper([string]$url, [string]$contentType) {
$this.SetOptions()
$this.client.BaseAddress = $url
$this.contentType = $contentType
$this.AddHeader("Accept", $this.contentType)
}
# adds a header to the client settings
[void] AddHeader([string]$key, [string]$value) {
$this.client.DefaultRequestHeaders.Add($key, $value)
}
# sets the base url of all requests
[void] SetUrl([string]$url) {
$this.client.BaseAddress = $url
}
# sets the content type of all requests
[void] SetContentType([string]$contentType) {
$this.AddHeader("Accept", $contentType)
}
# get request, returns a string
[string] Get([string]$path) {
$result = $this.client.GetStringAsync($path).GetAwaiter().GetResult()
return $result
}
# delete request without a payload, returns a string
[string] Delete([string]$path) {
$result = $this.client.DeleteAsync($path).GetAwaiter().GetResult()
return $result
}
# delete request with a payload, returns a string
[string] Delete([string]$path, $deleteContent) {
$content = [System.Net.Http.HttpRequestMessage]::new("DELETE", $path)
$content.Content = [System.Net.Http.StringContent]::new($deleteContent, [System.Text.Encoding]::UTF8, $this.contentType)
$result = $this.client.SendAsync($content).GetAwaiter().GetResult()
return $result.Content.ReadAsStringAsync().Result
}
# post request, returns a string
[string] Post([string]$path, $postContent) {
$content = [System.Net.Http.HttpRequestMessage]::new("POST", $path)
$content.Content = [System.Net.Http.StringContent]::new($postContent, [System.Text.Encoding]::UTF8, $this.contentType)
$result = $this.client.SendAsync($content).GetAwaiter().GetResult()
return $result.Content.ReadAsStringAsync().Result
}
# put request, returns a string
[string] Put([string]$path, $putContent) {
$content = [System.Net.Http.HttpRequestMessage]::new("PUT", $path)
$content.Content = [System.Net.Http.StringContent]::new($putContent, [System.Text.Encoding]::UTF8, $this.contentType)
$result = $this.client.SendAsync($content).GetAwaiter().GetResult()
return $result.Content.ReadAsStringAsync().Result
}
# internal method to set http options, enforces at tls1.2 & tls1.3, accepts any certificate without validation
hidden [void] SetOptions() {
$this.handler = [System.Net.Http.HttpClientHandler]::new()
$this.handler.ServerCertificateCustomValidationCallback = [System.Net.Http.HttpClientHandler]::DangerousAcceptAnyServerCertificateValidator
$this.handler.SslProtocols = [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls13
$this.client = [System.Net.Http.HttpClient]::new($this.handler)
}
}