github.com/grokify/go-ringcentral-client@v0.3.31/office/v0/examples/legacy2rest/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "log" 8 "net/http" 9 "os" 10 11 "github.com/grokify/goauth" 12 "github.com/grokify/goauth/authutil" 13 "github.com/grokify/mogo/net/http/httputilmore" 14 "github.com/joho/godotenv" 15 16 rc "github.com/grokify/go-ringcentral-client/office/v1/client" 17 ru "github.com/grokify/go-ringcentral-client/office/v1/util" 18 ) 19 20 type Handler struct { 21 AppPort int 22 APIClient *rc.APIClient 23 AppCredentials *goauth.CredentialsOAuth2 24 } 25 26 func (h *Handler) RingOut(res http.ResponseWriter, req *http.Request) { 27 // reqUtil := nhu.RequestUtil{Request: req} 28 29 pwdCredentials := goauth.CredentialsOAuth2{ 30 ClientID: h.AppCredentials.ClientID, 31 ClientSecret: h.AppCredentials.ClientSecret, 32 ServerURL: h.AppCredentials.ServerURL, 33 Endpoint: h.AppCredentials.Endpoint, 34 GrantType: authutil.GrantTypePassword, 35 Username: httputilmore.GetReqQueryParam(req, "username"), 36 Password: httputilmore.GetReqQueryParam(req, "password"), 37 TokenBodyOpts: map[string][]string{ 38 "refresh_token_ttl": {"-1"}}, 39 // RefreshTokenTTL: int64(-1), 40 } 41 42 ringOut := ru.RingOutRequest{ 43 To: httputilmore.GetReqQueryParam(req, "to"), 44 From: httputilmore.GetReqQueryParam(req, "from"), 45 CallerId: httputilmore.GetReqQueryParam(req, "clid"), 46 } 47 48 log.Printf("%v\n", ringOut) 49 50 apiClient, err := ru.NewApiClientPassword(pwdCredentials) 51 if err != nil { 52 res.WriteHeader(http.StatusBadRequest) 53 return 54 } 55 56 info, resp, err := apiClient.RingOutApi.MakeRingOutCallNew( 57 context.Background(), "~", "~", *ringOut.Body(), 58 ) 59 if err != nil { 60 res.WriteHeader(http.StatusBadRequest) 61 return 62 } else if resp.StatusCode >= 500 { 63 res.WriteHeader(http.StatusInternalServerError) 64 return 65 } else if resp.StatusCode >= 400 { 66 res.WriteHeader(http.StatusBadRequest) 67 return 68 } else if resp.StatusCode >= 300 { 69 res.WriteHeader(http.StatusInternalServerError) 70 return 71 } 72 73 bytes, err := json.Marshal(info) 74 if err != nil { 75 res.WriteHeader(http.StatusInternalServerError) 76 return 77 } 78 79 res.Header().Set(httputilmore.HeaderContentType, httputilmore.ContentTypeAppJSONUtf8) 80 res.Write(bytes) 81 } 82 83 func serveNetHttp(handler Handler) { 84 mux := http.NewServeMux() 85 mux.HandleFunc("/ringout.asp", http.HandlerFunc(handler.RingOut)) 86 mux.HandleFunc("/ringout.asp/", http.HandlerFunc(handler.RingOut)) 87 88 done := make(chan bool) 89 go http.ListenAndServe(fmt.Sprintf(":%v", handler.AppPort), mux) 90 log.Printf("Server listening on port %v", handler.AppPort) 91 <-done 92 } 93 94 func main() { 95 err := godotenv.Load(os.Getenv("ENV_PATH")) 96 if err != nil { 97 panic(err) 98 } 99 100 handler := Handler{ 101 AppPort: 8080, 102 AppCredentials: &goauth.CredentialsOAuth2{ 103 ServerURL: os.Getenv("RINGCENTRAL_SERVER_URL"), 104 ClientID: os.Getenv("RINGCENTRAL_CLIENT_ID"), 105 ClientSecret: os.Getenv("RINGCENTRAL_CLIENT_SECRET"), 106 }, 107 } 108 109 serveNetHttp(handler) 110 }