1
+ // Copyright (c) David Pine. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ namespace HaveIBeenPwned . Client ;
5
+
6
+ internal sealed partial class DefaultPwnedClient
7
+ {
8
+ /// <inheritdoc cref="IPwnedBreachesClient.GetBreachAsync(string)" />
9
+ async Task < BreachDetails ? > IPwnedBreachesClient . GetBreachAsync ( string breachName )
10
+ {
11
+ if ( string . IsNullOrWhiteSpace ( breachName ) )
12
+ {
13
+ throw new ArgumentException (
14
+ "The breachName cannot be either null, empty or whitespace." , nameof ( breachName ) ) ;
15
+ }
16
+
17
+ try
18
+ {
19
+ var client = _httpClientFactory . CreateClient ( HibpClient ) ;
20
+ var breachDetails =
21
+ await client . GetFromJsonAsync < BreachDetails > (
22
+ $ "breach/{ breachName } ") ;
23
+
24
+ return breachDetails ;
25
+ }
26
+ catch ( Exception ex )
27
+ {
28
+ _logger . LogError ( ex , ex . Message ) ;
29
+
30
+ return null ! ;
31
+ }
32
+ }
33
+
34
+ /// <inheritdoc cref="IPwnedBreachesClient.GetBreachesAsync(string?)" />
35
+ async Task < BreachHeader [ ] > IPwnedBreachesClient . GetBreachesAsync ( string ? domain )
36
+ {
37
+ try
38
+ {
39
+ var client = _httpClientFactory . CreateClient ( HibpClient ) ;
40
+ var queryString = string . IsNullOrWhiteSpace ( domain )
41
+ ? ""
42
+ : $ "?domain={ domain } ";
43
+
44
+ var breachHeaders =
45
+ await client . GetFromJsonAsync < BreachHeader [ ] > (
46
+ $ "breaches{ queryString } ") ;
47
+
48
+ return breachHeaders ?? Array . Empty < BreachHeader > ( ) ;
49
+ }
50
+ catch ( Exception ex )
51
+ {
52
+ _logger . LogError ( ex , ex . Message ) ;
53
+
54
+ return Array . Empty < BreachHeader > ( ) ;
55
+ }
56
+ }
57
+
58
+ /// <inheritdoc cref="IPwnedBreachesClient.GetBreachesForAccountAsync(string)" />
59
+ async Task < BreachDetails [ ] > IPwnedBreachesClient . GetBreachesForAccountAsync ( string account )
60
+ {
61
+ if ( string . IsNullOrWhiteSpace ( account ) )
62
+ {
63
+ throw new ArgumentException (
64
+ "The account cannot be either null, empty or whitespace." , nameof ( account ) ) ;
65
+ }
66
+
67
+ try
68
+ {
69
+ var client = _httpClientFactory . CreateClient ( HibpClient ) ;
70
+ var breachDetails =
71
+ await client . GetFromJsonAsync < BreachDetails [ ] > (
72
+ $ "breachedaccount/{ HttpUtility . UrlEncode ( account ) } ?truncateResponse=false") ;
73
+
74
+ return breachDetails ?? Array . Empty < BreachDetails > ( ) ;
75
+ }
76
+ catch ( Exception ex )
77
+ {
78
+ _logger . LogError ( ex , ex . Message ) ;
79
+
80
+ return Array . Empty < BreachDetails > ( ) ;
81
+ }
82
+ }
83
+
84
+ /// <inheritdoc cref="IPwnedBreachesClient.GetBreachHeadersForAccountAsync(string)" />
85
+ async Task < BreachHeader [ ] > IPwnedBreachesClient . GetBreachHeadersForAccountAsync ( string account )
86
+ {
87
+ if ( string . IsNullOrWhiteSpace ( account ) )
88
+ {
89
+ throw new ArgumentException (
90
+ "The account cannot be either null, empty or whitespace." , nameof ( account ) ) ;
91
+ }
92
+
93
+ try
94
+ {
95
+ var client = _httpClientFactory . CreateClient ( HibpClient ) ;
96
+ var breachDetails =
97
+ await client . GetFromJsonAsync < BreachDetails [ ] > (
98
+ $ "breachedaccount/{ HttpUtility . UrlEncode ( account ) } ?truncateResponse=true") ;
99
+
100
+ return breachDetails ?? Array . Empty < BreachDetails > ( ) ;
101
+ }
102
+ catch ( Exception ex )
103
+ {
104
+ _logger . LogError ( ex , ex . Message ) ;
105
+
106
+ return Array . Empty < BreachDetails > ( ) ;
107
+ }
108
+ }
109
+
110
+ /// <inheritdoc cref="IPwnedBreachesClient.GetDataClassesAsync" />
111
+ async Task < string [ ] > IPwnedBreachesClient . GetDataClassesAsync ( )
112
+ {
113
+ try
114
+ {
115
+ var client = _httpClientFactory . CreateClient ( HibpClient ) ;
116
+ var dataClasses =
117
+ await client . GetFromJsonAsync < string [ ] > ( "dataclasses" ) ;
118
+
119
+ return dataClasses ?? Array . Empty < string > ( ) ;
120
+ }
121
+ catch ( Exception ex )
122
+ {
123
+ _logger . LogError ( ex , ex . Message ) ;
124
+
125
+ return Array . Empty < string > ( ) ;
126
+ }
127
+ }
128
+ }
0 commit comments