From ff50b0e1382ba0214300ffb8eb467a78dae5b803 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 28 Aug 2017 14:19:00 -0500 Subject: add client4 apis needed for jira plugin (#7292) --- plugin/rpcplugin/api.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 4 deletions(-) (limited to 'plugin/rpcplugin/api.go') diff --git a/plugin/rpcplugin/api.go b/plugin/rpcplugin/api.go index 84eb3baae..e1a297b0a 100644 --- a/plugin/rpcplugin/api.go +++ b/plugin/rpcplugin/api.go @@ -3,8 +3,10 @@ package rpcplugin import ( "encoding/json" "io" + "net/http" "net/rpc" + "github.com/mattermost/platform/model" "github.com/mattermost/platform/plugin" ) @@ -13,9 +15,9 @@ type LocalAPI struct { muxer *Muxer } -func (h *LocalAPI) LoadPluginConfiguration(args struct{}, reply *[]byte) error { +func (api *LocalAPI) LoadPluginConfiguration(args struct{}, reply *[]byte) error { var config interface{} - if err := h.api.LoadPluginConfiguration(&config); err != nil { + if err := api.api.LoadPluginConfiguration(&config); err != nil { return err } b, err := json.Marshal(config) @@ -26,6 +28,67 @@ func (h *LocalAPI) LoadPluginConfiguration(args struct{}, reply *[]byte) error { return nil } +type APITeamReply struct { + Team *model.Team + Error *model.AppError +} + +func (api *LocalAPI) GetTeamByName(args string, reply *APITeamReply) error { + team, err := api.api.GetTeamByName(args) + *reply = APITeamReply{ + Team: team, + Error: err, + } + return nil +} + +type APIUserReply struct { + User *model.User + Error *model.AppError +} + +func (api *LocalAPI) GetUserByUsername(args string, reply *APIUserReply) error { + user, err := api.api.GetUserByUsername(args) + *reply = APIUserReply{ + User: user, + Error: err, + } + return nil +} + +type APIGetChannelByNameArgs struct { + Name string + TeamId string +} + +type APIChannelReply struct { + Channel *model.Channel + Error *model.AppError +} + +func (api *LocalAPI) GetChannelByName(args *APIGetChannelByNameArgs, reply *APIChannelReply) error { + channel, err := api.api.GetChannelByName(args.Name, args.TeamId) + *reply = APIChannelReply{ + Channel: channel, + Error: err, + } + return nil +} + +type APIPostReply struct { + Post *model.Post + Error *model.AppError +} + +func (api *LocalAPI) CreatePost(args *model.Post, reply *APIPostReply) error { + post, err := api.api.CreatePost(args) + *reply = APIPostReply{ + Post: post, + Error: err, + } + return nil +} + func ServeAPI(api plugin.API, conn io.ReadWriteCloser, muxer *Muxer) { server := rpc.NewServer() server.Register(&LocalAPI{ @@ -42,14 +105,49 @@ type RemoteAPI struct { var _ plugin.API = (*RemoteAPI)(nil) -func (h *RemoteAPI) LoadPluginConfiguration(dest interface{}) error { +func (api *RemoteAPI) LoadPluginConfiguration(dest interface{}) error { var config []byte - if err := h.client.Call("LocalAPI.LoadPluginConfiguration", struct{}{}, &config); err != nil { + if err := api.client.Call("LocalAPI.LoadPluginConfiguration", struct{}{}, &config); err != nil { return err } return json.Unmarshal(config, dest) } +func (api *RemoteAPI) GetTeamByName(name string) (*model.Team, *model.AppError) { + var reply APITeamReply + if err := api.client.Call("LocalAPI.GetTeamByName", name, &reply); err != nil { + return nil, model.NewAppError("RemoteAPI.GetTeamByName", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + return reply.Team, reply.Error +} + +func (api *RemoteAPI) GetUserByUsername(name string) (*model.User, *model.AppError) { + var reply APIUserReply + if err := api.client.Call("LocalAPI.GetUserByUsername", name, &reply); err != nil { + return nil, model.NewAppError("RemoteAPI.GetUserByUsername", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + return reply.User, reply.Error +} + +func (api *RemoteAPI) GetChannelByName(name, teamId string) (*model.Channel, *model.AppError) { + var reply APIChannelReply + if err := api.client.Call("LocalAPI.GetChannelByName", &APIGetChannelByNameArgs{ + Name: name, + TeamId: teamId, + }, &reply); err != nil { + return nil, model.NewAppError("RemoteAPI.GetChannelByName", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + return reply.Channel, reply.Error +} + +func (api *RemoteAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError) { + var reply APIPostReply + if err := api.client.Call("LocalAPI.CreatePost", post, &reply); err != nil { + return nil, model.NewAppError("RemoteAPI.CreatePost", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + return reply.Post, reply.Error +} + func (h *RemoteAPI) Close() error { return h.client.Close() } -- cgit v1.2.3-1-g7c22