From 4fe152476e9b35925cbdf7eb45db9ef7941e9adf Mon Sep 17 00:00:00 2001 From: M Anshori Date: Tue, 20 Jul 2021 02:41:41 +0700 Subject: [PATCH 1/3] fix: return error nil get resp restrict user --- request.go | 2 +- users.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/request.go b/request.go index d42b42e8..ee369e09 100644 --- a/request.go +++ b/request.go @@ -116,7 +116,7 @@ func (insta *Instagram) sendRequest(o *reqOptions) (body []byte, err error) { if err == nil { err = isError(resp.StatusCode, body) } - return body, err + return body, nil } func isError(code int, body []byte) (err error) { diff --git a/users.go b/users.go index 9a02c044..0ed8c00f 100644 --- a/users.go +++ b/users.go @@ -107,8 +107,9 @@ func (users *Users) setValues() { } type userResp struct { - Status string `json:"status"` - User User `json:"user"` + Status string `json:"status"` + User User `json:"user"` + FeedbackMessage string `json:"feedback_message"` } // User is the representation of instagram's user profile From 9bf653493f917ac0b27109ed869370b578b04424 Mon Sep 17 00:00:00 2001 From: M Anshori Date: Tue, 20 Jul 2021 10:34:16 +0700 Subject: [PATCH 2/3] fix: change scenario --- request.go | 8 +++++++- types.go | 18 +++++++++++++++--- users.go | 5 ++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/request.go b/request.go index ee369e09..4010668b 100644 --- a/request.go +++ b/request.go @@ -3,6 +3,7 @@ package goinsta import ( "bytes" "encoding/json" + "errors" "fmt" "io/ioutil" "math/rand" @@ -115,8 +116,11 @@ func (insta *Instagram) sendRequest(o *reqOptions) (body []byte, err error) { body, err = ioutil.ReadAll(resp.Body) if err == nil { err = isError(resp.StatusCode, body) + } else if resp.StatusCode == http.StatusBadRequest { + err = isError(resp.StatusCode, body) } - return body, nil + + return body, err } func isError(code int, body []byte) (err error) { @@ -136,6 +140,8 @@ func isError(code int, body []byte) (err error) { if ierr.Message == "challenge_required" { return ierr.ChallengeError + } else if ierr.Message == "feedback_required" { + return errors.New("fail: " + ierr.FeedbackMessage) } if err == nil && ierr.Message != "" { diff --git a/types.go b/types.go index 65547fcc..3082b124 100644 --- a/types.go +++ b/types.go @@ -52,9 +52,10 @@ func (e ErrorN) Error() string { // Error400 is error returned by HTTP 400 status code. type Error400 struct { ChallengeError - Action string `json:"action"` - StatusCode string `json:"status_code"` - Payload struct { + FeedbackMessage string `json:"feedback_message"` + Action string `json:"action"` + StatusCode string `json:"status_code"` + Payload struct { ClientContext string `json:"client_context"` Message string `json:"message"` } `json:"payload"` @@ -149,6 +150,17 @@ type Friendship struct { IsMutingReel bool `json:"is_muting_reel"` } +// SavedMedia stores the information about media being saved before in my account. +type SavedMedia struct { + Items []struct { + Media Item `json:"media"` + } `json:"items"` + NumResults int `json:"num_results"` + MoreAvailable bool `json:"more_available"` + AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"` + Status string `json:"status"` +} + // Images are different quality images type Images struct { Versions []Candidate `json:"candidates"` diff --git a/users.go b/users.go index 0ed8c00f..9a02c044 100644 --- a/users.go +++ b/users.go @@ -107,9 +107,8 @@ func (users *Users) setValues() { } type userResp struct { - Status string `json:"status"` - User User `json:"user"` - FeedbackMessage string `json:"feedback_message"` + Status string `json:"status"` + User User `json:"user"` } // User is the representation of instagram's user profile From f3824463e9a65213c0a47585398d02975e94a950 Mon Sep 17 00:00:00 2001 From: M Anshori Date: Tue, 20 Jul 2021 10:36:18 +0700 Subject: [PATCH 3/3] chore: remove unused model --- types.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/types.go b/types.go index 3082b124..3a05c416 100644 --- a/types.go +++ b/types.go @@ -150,17 +150,6 @@ type Friendship struct { IsMutingReel bool `json:"is_muting_reel"` } -// SavedMedia stores the information about media being saved before in my account. -type SavedMedia struct { - Items []struct { - Media Item `json:"media"` - } `json:"items"` - NumResults int `json:"num_results"` - MoreAvailable bool `json:"more_available"` - AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"` - Status string `json:"status"` -} - // Images are different quality images type Images struct { Versions []Candidate `json:"candidates"`