-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor repositories adapters from the native http go to the resty+g…
…json
- Loading branch information
1 parent
56d48cf
commit c27fd1a
Showing
8 changed files
with
107 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"github.com/go-resty/resty/v2" | ||
"github.com/tidwall/gjson" | ||
"net/http" | ||
) | ||
|
||
type LandpadRepository struct { | ||
apiBaseUri string | ||
client *resty.Client | ||
} | ||
|
||
const landpadActiveStatus = "active" | ||
|
||
func NewLandpadRepository(apiBaseUri string) *LandpadRepository { | ||
func NewLandpadRepository(apiBaseURL string) *LandpadRepository { | ||
return &LandpadRepository{ | ||
apiBaseUri: apiBaseUri, | ||
client: resty.New(). | ||
SetBaseURL(apiBaseURL). | ||
SetHeader("Content-Type", "application/json"), | ||
} | ||
} | ||
|
||
func (r *LandpadRepository) IsExists(id string) (bool, error) { | ||
_, statusCode, err := r.getLandpad(id) | ||
resp, err := r.client.R(). | ||
SetPathParam("landpadId", id). | ||
Get("/v4/landpads/{landpadId}") | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return statusCode == http.StatusOK, nil | ||
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { | ||
return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) | ||
} | ||
return resp.StatusCode() == http.StatusOK, nil | ||
} | ||
|
||
func (r *LandpadRepository) IsActive(id string) (bool, error) { | ||
lp, _, err := r.getLandpad(id) | ||
resp, err := r.client.R(). | ||
SetPathParam("landpadId", id). | ||
Get("/v4/landpads/{landpadId}") | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return lp.Status == landpadActiveStatus, nil | ||
} | ||
|
||
func (r *LandpadRepository) getLandpad(id string) (lp *landpad, statusCode int, err error) { | ||
resp, err := http.Get(fmt.Sprintf("%s/v4/landpads/%s", r.apiBaseUri, id)) | ||
|
||
if err != nil { | ||
return nil, 0, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { | ||
return nil, resp.StatusCode, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode) | ||
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNotFound { | ||
return false, fmt.Errorf("got unexpected http status code: %d", resp.StatusCode()) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
lp = new(landpad) | ||
err = json.Unmarshal(body, lp) | ||
if err != nil { | ||
return lp, resp.StatusCode, err | ||
} | ||
|
||
return lp, resp.StatusCode, nil | ||
} | ||
|
||
type landpad struct { | ||
Status string | ||
respData := gjson.ParseBytes(resp.Body()) | ||
return respData.Get("Status").String() == landpadActiveStatus, nil | ||
} |
Oops, something went wrong.