Skip to content

Commit

Permalink
Merge pull request #35 from freman/main
Browse files Browse the repository at this point in the history
Add latestbreach api
  • Loading branch information
wneessen authored May 8, 2024
2 parents 1bff712 + f2f530e commit bff329f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions breach.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ func (b *BreachAPI) BreachByName(n string, options ...BreachOption) (*Breach, *h
return bd, hr, nil
}

// LatestBreach returns the single most recent breach
func (b *BreachAPI) LatestBreach() (*Breach, *http.Response, error) {
au := fmt.Sprintf("%s/latestbreach", BaseURL)
hb, hr, err := b.hibp.HTTPResBody(http.MethodGet, au, nil)
if err != nil {
return nil, hr, err
}

var bd *Breach
if err := json.Unmarshal(hb, &bd); err != nil {
return nil, hr, err
}

return bd, hr, nil
}

// DataClasses are attribute of a record compromised in a breach. This method returns a list of strings
// with all registered data classes known to HIBP
func (b *BreachAPI) DataClasses() ([]string, *http.Response, error) {
Expand Down
14 changes: 14 additions & 0 deletions breach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ func TestBreachAPI_BreachByName_Errors(t *testing.T) {
}
}

// TestBreachAPI_LatestBreach tests the LatestBreach method of the breaches API
func TestBreachAPI_LatestBreach(t *testing.T) {
hc := New()
breach, _, err := hc.BreachAPI.LatestBreach()
if err != nil {
t.Error(err)
return
}

if breach == nil {
t.Error("No breach returned")
}
}

// TestBreachAPI_DataClasses tests the DataClasses() method of the breaches API
func TestBreachAPI_DataClasses(t *testing.T) {
hc := New()
Expand Down

0 comments on commit bff329f

Please sign in to comment.