Last active
December 12, 2018 09:50
-
-
Save slifer2015/73faef0d92566798111e6eb39f6e831f to your computer and use it in GitHub Desktop.
api function handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api | |
import ( | |
"bytes" | |
"context" | |
"encoding/json" | |
"errors" | |
"io/ioutil" | |
"net/http" | |
"strings" | |
"time" | |
"git.snappfood.ir/golang/services/array" | |
"git.snappfood.ir/golang/services/assert" | |
) | |
// Call helper for api calls | |
func Call(ctx context.Context, method, url string, headers map[string]string, timeout time.Duration, pl interface{}) ([]byte, int, error) { | |
d, err := json.Marshal(pl) | |
assert.Nil(err) | |
b := bytes.NewReader(d) | |
method = strings.ToUpper(method) | |
if array.StringInArray(method, "GET", "DELETE") { | |
b = nil | |
} | |
r, err := http.NewRequest(method, url, b) | |
if err != nil { | |
return nil, 0, errors.New("error while creating request") | |
} | |
for i := range headers { | |
r.Header.Set(i, headers[i]) | |
} | |
nCtx, _ := context.WithTimeout(ctx, timeout) | |
resp, err := http.DefaultClient.Do(r.WithContext(nCtx)) | |
if err != nil { | |
return nil, 0, errors.New("error in return response") | |
} | |
data, err := ioutil.ReadAll(resp.Body) | |
defer func() { | |
_ = resp.Body.Close() | |
}() | |
if err != nil { | |
return nil, resp.StatusCode, errors.New("error in reading response") | |
} | |
return data, resp.StatusCode, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runtime/debug.Stack(0xc0000911e8, 0x8c7640, 0xd19b40) | |
/usr/local/go/src/runtime/debug/stack.go:24 +0xa7 | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/safe.Routine.func1(0xc000091cc8, 0x1, 0x1) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/safe/safe.go:64 +0x6e | |
panic(0x8c7640, 0xd19b40) | |
/usr/local/go/src/runtime/panic.go:513 +0x1b9 | |
bytes.(*Reader).Len(...) | |
/usr/local/go/src/bytes/reader.go:26 | |
net/http.NewRequest(0x94622a, 0x3, 0xc00002a720, 0x52, 0x9cd5c0, 0x0, 0x0, 0xc000091498, 0x44459f) | |
/usr/local/go/src/net/http/request.go:832 +0x3fb | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/api.Call(0x9d27c0, 0xc000256600, 0x94622a, 0x3, 0xc00002a720, 0x52, 0xc000091648, 0x12a05f200, 0x0, 0x0, ...) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/api/call.go:26 +0x201 | |
git.snappfood.ir/golang/consent/modules/user/controllers.getLogin(0x9d27c0, 0xc000256600, 0x9d17c0, 0xc00000a1e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/modules/user/controllers/login.go:61 +0x21c | |
git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler.HandlerFuncC.ServeHTTPC(0x965c20, 0x9d27c0, 0xc000256600, 0x9d17c0, 0xc00000a1e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler/xhandler.go:32 +0x58 | |
git.snappfood.ir/golang/consent/vendor/github.com/rs/xmux.(*Mux).ServeHTTPC(0xc00027c0c0, 0x9d27c0, 0xc000256600, 0x9d17c0, 0xc00000a1e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/github.com/rs/xmux/mux.go:315 +0x97e | |
git.snappfood.ir/golang/consent/vendor/github.com/rs/xmux.(*Mux).ServeHTTPC-fm(0x9d27c0, 0xc000256600, 0x9d17c0, 0xc00000a1e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/router/init.go:32 +0x5c | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware.Logger.func1(0x9d27c0, 0xc000256600, 0x9d2300, 0xc0002500e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware/logger.go:43 +0x308 | |
git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler.HandlerFuncC.ServeHTTPC(0xc000272080, 0x9d2740, 0xc0000a6010, 0x9d2300, 0xc0002500e0, 0xc0002d6100) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler/xhandler.go:32 +0x58 | |
git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler.New.func1(0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/github.com/rs/xhandler/new.go:17 +0x103 | |
net/http.HandlerFunc.ServeHTTP(0xc000276120, 0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/usr/local/go/src/net/http/server.go:1964 +0x44 | |
net/http.Handler.ServeHTTP-fm(0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/router/init.go:32 +0x4d | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware.Recovery.func1.1() | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware/recovery.go:15 +0x40 | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/safe.Routine(0xc0000c9ce8, 0xc0000c9cc8, 0x1, 0x1) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/safe/safe.go:70 +0x61 | |
git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware.Recovery.func1(0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/home/dara/projects/src/git.snappfood.ir/golang/consent/vendor/git.snappfood.ir/golang/services/framework/middleware/recovery.go:14 +0x11a | |
net/http.HandlerFunc.ServeHTTP(0xc000272090, 0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/usr/local/go/src/net/http/server.go:1964 +0x44 | |
net/http.serverHandler.ServeHTTP(0xc0002781a0, 0x9d2300, 0xc0002500e0, 0xc0002d6000) | |
/usr/local/go/src/net/http/server.go:2741 +0xab | |
net/http.(*conn).serve(0xc0002d0000, 0x9d2700, 0xc000036300) | |
/usr/local/go/src/net/http/server.go:1847 +0x646 | |
created by net/http.(*Server).Serve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment