first
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Errors
|
||||
var (
|
||||
ErrAPICall = errors.New("API call fail")
|
||||
)
|
||||
|
||||
// GenRequest -
|
||||
func GenRequest(targetURL, method, bearerToken string, headers map[string]string, body io.Reader) (*http.Request, error) {
|
||||
logrus.Infof("[APIS][GenRequest] Generate Request from url : %s\n", targetURL)
|
||||
req, err := http.NewRequest(method, targetURL, body)
|
||||
if err != nil {
|
||||
logrus.Errorf("[APIS][GenRequest] Create request fail : %s\n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(headers) > 0 {
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if len(bearerToken) > 0 {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// SendRequest -
|
||||
func SendRequest(req *http.Request, data interface{}) (status int, b []byte, err error) {
|
||||
logrus.Infof("[APIS][SendRequest] Send Request\n")
|
||||
if req == nil {
|
||||
logrus.Errorf("[APIS][SendRequest] Request Object Empty\n")
|
||||
return 0, nil, errors.New("request object is empty")
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logrus.Errorf("[APIS][SendRequest] HTTP Request fail : %s\n", err.Error())
|
||||
return -1, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logrus.Errorf("[APIS][SendRequest] Read RespBody fail : %s\n", err.Error())
|
||||
return resp.StatusCode, b, err
|
||||
}
|
||||
if data != nil {
|
||||
t := reflect.TypeOf(data)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
|
||||
err = json.Unmarshal(b, data)
|
||||
if err != nil {
|
||||
logrus.Errorf("[APIS][SendRequest] JSON parse fail : %s\n", err.Error())
|
||||
return resp.StatusCode, b, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resp.StatusCode, b, nil
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package google
|
||||
|
||||
// GetCalendarList -
|
||||
func GetCalendarList() (err error) {
|
||||
p := "/calendar/v3/users/me/calendarList"
|
||||
u, err := getURL(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = u
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package google
|
||||
|
||||
import "net/url"
|
||||
|
||||
var baseURL = "https://www.googleapis.com/"
|
||||
|
||||
func getURL(p string) (string, error) {
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u, err = u.Parse(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func getHeaders() map[string]string {
|
||||
obj := make(map[string]string)
|
||||
// conf := config.Get()
|
||||
|
||||
return obj
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package calendar
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
)
|
||||
|
||||
// Draw -
|
||||
func Draw(w, h int) *image.Image {
|
||||
dc := gg.NewContext(w, h)
|
||||
_ = dc
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
"github.com/otakukaze/envconfig"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Google config
|
||||
type Google struct {
|
||||
APIKey string `yaml:"api_key" env:"GOOGLE_API_KEY"`
|
||||
}
|
||||
|
||||
// Config main struct
|
||||
type Config struct {
|
||||
Port int `yaml:"port" env:"PORT"`
|
||||
Google Google `yaml:"google"`
|
||||
}
|
||||
|
||||
var conf *Config
|
||||
|
||||
// Load config from file and env
|
||||
func Load(p ...string) (err error) {
|
||||
fp := ""
|
||||
if len(p) > 0 && len(p[0]) > 0 {
|
||||
fp = p[0]
|
||||
} else {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fp = path.Join(wd, "config.yml")
|
||||
}
|
||||
|
||||
fp = utils.ParsePath(fp)
|
||||
if !utils.CheckExists(fp, false) {
|
||||
return errors.New("config file not found")
|
||||
}
|
||||
|
||||
conf = &Config{}
|
||||
|
||||
b, err := ioutil.ReadFile(fp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(b, conf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
envconfig.Parse(conf)
|
||||
return
|
||||
}
|
||||
|
||||
// Get config struct
|
||||
func Get() *Config { return conf }
|
||||
@@ -0,0 +1,70 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
)
|
||||
|
||||
// Storage -
|
||||
type Storage struct {
|
||||
m map[string]interface{}
|
||||
filePath string
|
||||
}
|
||||
|
||||
var s *Storage
|
||||
|
||||
// New storage
|
||||
func New() *Storage {
|
||||
if s == nil {
|
||||
s = &Storage{
|
||||
m: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Get storage
|
||||
func Get() *Storage { return s }
|
||||
|
||||
// Load storage data from file
|
||||
func (p *Storage) Load(filePath string) (err error) {
|
||||
filePath = utils.ParsePath(filePath)
|
||||
if !utils.CheckExists(filePath, false) {
|
||||
return errors.New("storage file not found")
|
||||
}
|
||||
p.filePath = filePath
|
||||
|
||||
b, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, &p.m)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Storage) Write() (err error) {
|
||||
b, err := json.Marshal(p.m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(p.filePath, b, 0664)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Get data by key
|
||||
func (p *Storage) Get(key string) (data interface{}, ok bool) {
|
||||
data, ok = p.m[key]
|
||||
return
|
||||
}
|
||||
|
||||
// Set data by key
|
||||
func (p *Storage) Set(key string, value interface{}) { p.m[key] = value }
|
||||
|
||||
// Delete data by key
|
||||
func (p *Storage) Delete(key string) { delete(p.m, key) }
|
||||
Reference in New Issue
Block a user