@@ -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
2727func 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
3937func (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
7163func (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