Skip to content

Commit 8f939d2

Browse files
internal/api -> pkg/api: make the api client public
After starting work on the desktop client, this was a necessary to use the same client there.
1 parent 8812da6 commit 8f939d2

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

cmd/root.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"os"
77
"time"
88

9-
"github.com/cipherbin/cipher-bin-cli/internal/api"
9+
"github.com/cipherbin/cipher-bin-cli/pkg/api"
1010
"github.com/spf13/cobra"
1111
)
1212

1313
// Version constant that represents the current build version
14-
const Version = "v.0.5.1"
14+
const Version = "v.0.5.2"
1515

1616
// Email will be hydrated with it's value if a user runs the create cmd with the
1717
// flag --email (or -e)
@@ -34,7 +34,6 @@ var APIClient *api.Client
3434

3535
func init() {
3636
client := http.Client{Timeout: 15 * time.Second}
37-
3837
browserBaseURL := "https://cipherb.in"
3938
apiBaseURL := "https://api.cipherb.in"
4039

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,28 @@ type CipherBinAPIClient interface {
2323
Do(req *http.Request) (*http.Response, error)
2424
}
2525

26-
// NewClient is a constructor for the ApiClient
26+
// NewClient is a constructor for the ApiClient and satifies the CipherBinAPIClient interface
2727
func NewClient(browserBaseURL, apiBaseURL string, client CipherBinAPIClient) (*Client, error) {
28-
apiClient := &Client{
28+
return &Client{
2929
CipherBinAPIClient: client,
3030
BrowserBaseURL: browserBaseURL,
3131
APIBaseURL: apiBaseURL,
32-
}
33-
34-
return apiClient, nil
32+
}, nil
3533
}
3634

37-
// PostMessage takes a msg of type *db.Message (this is what the server
38-
// uses and will expect) and posts it to the cipherbin api
35+
// PostMessage takes a msg of type *db.Message (this is what the server uses and will expect)
36+
// and posts it to the live cipherbin api
3937
func (c *Client) PostMessage(msg *db.Message) error {
4038
payloadBytes, err := json.Marshal(msg)
4139
if err != nil {
4240
return err
4341
}
4442

45-
req, err := http.NewRequest(
46-
"POST",
47-
fmt.Sprintf("%s%s", c.APIBaseURL, "/msg"),
48-
bytes.NewBuffer(payloadBytes),
49-
)
43+
req, err := http.NewRequest("POST", c.APIBaseURL+"/msg", bytes.NewBuffer(payloadBytes))
5044
if err != nil {
5145
return err
5246
}
5347
defer req.Body.Close()
54-
5548
req.Header.Set("Content-Type", "application/json")
5649

5750
res, err := c.Do(req)
@@ -63,11 +56,10 @@ func (c *Client) PostMessage(msg *db.Message) error {
6356
if res.StatusCode != http.StatusOK {
6457
return fmt.Errorf("Error: response status: %d", res.StatusCode)
6558
}
66-
6759
return nil
6860
}
6961

70-
// GetMessage
62+
// GetMessage simply takes a cipherb.in public URL string and returns the appropriate encrypted message
7163
func (c *Client) GetMessage(url string) (*app.MessageResponse, error) {
7264
req, err := http.NewRequest("GET", url, nil)
7365
if err != nil {
@@ -83,14 +75,12 @@ func (c *Client) GetMessage(url string) (*app.MessageResponse, error) {
8375
if res.StatusCode == http.StatusNotFound {
8476
return nil, errors.New("Sorry, this message has either already been viewed and destroyed or it never existed at all")
8577
}
86-
8778
if res.StatusCode != http.StatusOK {
8879
return nil, fmt.Errorf("Error: response status: %d", res.StatusCode)
8980
}
9081

9182
var r app.MessageResponse
92-
err = json.NewDecoder(res.Body).Decode(&r)
93-
if err != nil {
83+
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
9484
return nil, err
9585
}
9686

0 commit comments

Comments
 (0)