Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limit support #86

Merged
merged 1 commit into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ type DebugInfo struct {
FacebookRev string // the x-fb-rev HTTP header.
}

// UsageInfo is the app usage (rate limit) information returned by facebook when rate limits are possible.
type UsageInfo struct {
App struct {
CallCount int `json:"call_count"`
TotalTime int `json:"total_time"`
TotalCPUTime int `json:"total_cputime"`
} `json:"app"`
Page struct {
CallCount int `json:"call_count"`
TotalTime int `json:"total_time"`
TotalCPUTime int `json:"total_cputime"`
} `json:"page"`
}

// DebugMessage is one debug message in "__debug__" of graph API response.
type DebugMessage struct {
Type string
Expand Down Expand Up @@ -433,6 +447,15 @@ func (res Result) DebugInfo() *DebugInfo {
return debugInfo
}

// UsageInfo returns app and page usage info (rate limits)
func (res Result) UsageInfo() *UsageInfo {
if usageInfo, ok := res["__usage__"]; ok {
ui := usageInfo.(UsageInfo)
return &ui
}
return nil
}

func (res Result) decode(v reflect.Value, fullName string) error {
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
Expand Down
14 changes: 14 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (session *Session) Request(request *http.Request) (res Result, err error) {

res, err = MakeResult(data)
session.addDebugInfo(res, response)
session.addUsageInfo(res, response)

if res != nil {
err = res.Err()
Expand Down Expand Up @@ -347,6 +348,7 @@ func (session *Session) graph(path string, method Method, params Params) (res Re
var response *http.Response
response, err = session.sendPostRequest(graphUrl, params, &res)
session.addDebugInfo(res, response)
session.addUsageInfo(res, response)

if res != nil {
err = res.Err()
Expand Down Expand Up @@ -561,6 +563,18 @@ func (session *Session) addDebugInfo(res Result, response *http.Response) Result
return res
}

func (session *Session) addUsageInfo(res Result, response *http.Response) Result {
var usageInfo UsageInfo
if response.Header.Get("X-App-Usage") != "" {
json.Unmarshal([]byte(response.Header.Get("X-App-Usage")), &usageInfo.App)
}
if response.Header.Get("X-Page-Usage") != "" {
json.Unmarshal([]byte(response.Header.Get("X-Page-Usage")), &usageInfo.Page)
}
res["__usage__"] = usageInfo
return res
}

// Context returns the session's context.
// To change the context, use `Session#WithContext`.
//
Expand Down