Skip to content

Commit

Permalink
Updated pointer dereferences to be a little bit safer
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsky committed Aug 6, 2020
1 parent 9deb6cc commit a71cf89
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func main() {
// The bytes of the PKCS#8 private key created on App Store Connect. Keep this key safe as you can only download it once.
privateKey = ioutil.ReadFile("path/to/key")

auth, err = asc.NewTokenConfig(keyID, issuerID, expiryDuration, privateKey)
if err != nil {
return nil, err
}
auth, err = asc.NewTokenConfig(keyID, issuerID, expiryDuration, privateKey)
if err != nil {
return nil, err
}
client := asc.NewClient(auth.Client())

// list all apps with the bundle ID "com.sky.MyApp" in the authenticated user's team
Expand Down
34 changes: 17 additions & 17 deletions asc/asc.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,29 @@ type Rate struct {
Remaining int `json:"remaining"`
}

// ErrorResponse contains information with error details that an API returns in the response body whenever the API request is not successful.
// ErrorResponse contains information with error details that an API returns in the
// response body whenever the API request is not successful.
type ErrorResponse struct {
Response *http.Response `json:"-"`
Errors *[]struct {
Code string `json:"code"`
Detail string `json:"detail"`
ID *string `json:"id,omitempty"`
Source *interface{} `json:"source,omitempty"`
Status string `json:"status"`
Title string `json:"title"`
} `json:"errors,omitempty"`
Response *http.Response `json:"-"`
Errors *[]ErrorResponseError `json:"errors,omitempty"`
}

// ErrorResponseError is a model used in ErrorResponse to describe a single error from the API
type ErrorResponseError struct {
Code string `json:"code"`
Detail string `json:"detail"`
ID *string `json:"id,omitempty"`
Source *interface{} `json:"source,omitempty"`
Status string `json:"status"`
Title string `json:"title"`
}

type service struct {
client *Client
}

// request is a common structure for a request body sent to the API
type request struct {
type writingRequestPayload struct {
Data interface{} `json:"data"`
}

Expand Down Expand Up @@ -213,7 +217,8 @@ func (c *Client) newRequest(method, path string, body interface{}, options ...re

buf := new(bytes.Buffer)
if body != nil {
err := json.NewEncoder(buf).Encode(wrapRequest(body))
payload := &writingRequestPayload{Data: body}
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -297,11 +302,6 @@ func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
return response, err
}

func wrapRequest(v interface{}) *request {
req := &request{Data: v}
return req
}

func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
response.Rate = parseRate(r)
Expand Down
7 changes: 4 additions & 3 deletions asc/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ func emailContainerJSON(email string) string {
}

func TestEmailMarshal(t *testing.T) {
want := emailContainerJSON("[email protected]")
b := newEmailContainer("[email protected]")
email := "[email protected]"
want := emailContainerJSON(email)
b := newEmailContainer(email)
got, err := json.Marshal(b)
assert.NoError(t, err)
assert.JSONEq(t, want, string(got))
Expand All @@ -86,7 +87,7 @@ func TestEmailMarshalInvalidEmail(t *testing.T) {

func TestEmailUnmarshal(t *testing.T) {
want := "[email protected]"
jsonStr := emailContainerJSON("[email protected]")
jsonStr := emailContainerJSON(want)
var b emailContainer
err := json.Unmarshal([]byte(jsonStr), &b)
assert.NoError(t, err)
Expand Down
15 changes: 13 additions & 2 deletions asc/upload_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (e UploadOperationError) Error() string {

// Chunk returns the bytes in the file from the given offset and with the given length
func (op *UploadOperation) Chunk(f *os.File) (io.Reader, error) {
if op.Offset == nil || op.Length == nil {
return nil, errors.New("could not establish bounds of upload operation")
}
_, err := f.Seek(int64(*op.Offset), 0)
if err != nil {
return nil, err
Expand All @@ -76,12 +79,20 @@ func (op *UploadOperation) Chunk(f *os.File) (io.Reader, error) {

// Request creates a new http.Request instance from the given UploadOperation and buffer
func (op *UploadOperation) Request(data io.Reader) (*http.Request, error) {
if op.Method == nil || op.URL == nil {
return nil, errors.New("could not establish destination of upload operation")
}
req, err := http.NewRequest(*op.Method, *op.URL, data)
if err != nil {
return nil, err
}
for _, h := range *op.RequestHeaders {
req.Header.Add(*h.Name, *h.Value)
if op.RequestHeaders != nil {
for _, h := range *op.RequestHeaders {
if h.Name == nil || h.Value == nil {
continue
}
req.Header.Add(*h.Name, *h.Value)
}
}
return req, nil
}
Expand Down

0 comments on commit a71cf89

Please sign in to comment.