matrixprotobuf

git clone git://archive.git.mtrnord.blog/MTRNord/matrixprotobuf.git
Log | Files | Refs | README | LICENSE

commit f146bc430f743b8b6b3275ac871a128340777406
parent f1a88348a765d7e78285a5c6dbd9041beb78e349
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 16 Sep 2017 22:13:22 +0200

Rework HTTP Client for the Versions Endpoint and let it get Data from an actual HS

Signed-off-by: MTRNord <mtrnord1@gmail.com>

Diffstat:
Asrc/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go | 5+++++
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto | 8++++++++
Msrc/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go | 37++++++++++++++++++++++++++++++++-----
Msrc/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go | 10+++++-----
Msrc/github.com/Nordgedanken/mpbProtoClient/client.go | 11++++++++++-
6 files changed, 168 insertions(+), 34 deletions(-)

diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go b/src/github.com/Nordgedanken/MatrixProtoBuf/jsonTypes/unauthorized.go @@ -0,0 +1,5 @@ +package jsonTypes + +type HSVersions struct { + Versions []string `json:"versions"` +} diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.pb.go @@ -10,6 +10,7 @@ It is generated from these files: It has these top-level messages: VersionsResponse Version + VersionRequest LoginRequest LoginResponse */ @@ -67,6 +68,22 @@ func (m *Version) GetVersion() string { return "" } +type VersionRequest struct { + Address string `protobuf:"bytes,1,opt,name=address" json:"address,omitempty"` +} + +func (m *VersionRequest) Reset() { *m = VersionRequest{} } +func (m *VersionRequest) String() string { return proto.CompactTextString(m) } +func (*VersionRequest) ProtoMessage() {} +func (*VersionRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } + +func (m *VersionRequest) GetAddress() string { + if m != nil { + return m.Address + } + return "" +} + type LoginRequest struct { Type string `protobuf:"bytes,1,opt,name=type" json:"type,omitempty"` User string `protobuf:"bytes,2,opt,name=user" json:"user,omitempty"` @@ -78,7 +95,7 @@ type LoginRequest struct { func (m *LoginRequest) Reset() { *m = LoginRequest{} } func (m *LoginRequest) String() string { return proto.CompactTextString(m) } func (*LoginRequest) ProtoMessage() {} -func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } +func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } func (m *LoginRequest) GetType() string { if m != nil { @@ -127,7 +144,7 @@ type LoginResponse struct { func (m *LoginResponse) Reset() { *m = LoginResponse{} } func (m *LoginResponse) String() string { return proto.CompactTextString(m) } func (*LoginResponse) ProtoMessage() {} -func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } +func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } func (m *LoginResponse) GetAccessToken() string { if m != nil { @@ -174,6 +191,7 @@ func (m *LoginResponse) GetError() string { func init() { proto.RegisterType((*VersionsResponse)(nil), "matrixProtos.VersionsResponse") proto.RegisterType((*Version)(nil), "matrixProtos.Version") + proto.RegisterType((*VersionRequest)(nil), "matrixProtos.VersionRequest") proto.RegisterType((*LoginRequest)(nil), "matrixProtos.LoginRequest") proto.RegisterType((*LoginResponse)(nil), "matrixProtos.LoginResponse") } @@ -186,6 +204,70 @@ var _ grpc.ClientConn // is compatible with the grpc package it is being compiled against. const _ = grpc.SupportPackageIsVersion4 +// Client API for VersioService service + +type VersioServiceClient interface { + Versions(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionsResponse, error) +} + +type versioServiceClient struct { + cc *grpc.ClientConn +} + +func NewVersioServiceClient(cc *grpc.ClientConn) VersioServiceClient { + return &versioServiceClient{cc} +} + +func (c *versioServiceClient) Versions(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionsResponse, error) { + out := new(VersionsResponse) + err := grpc.Invoke(ctx, "/matrixProtos.VersioService/Versions", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for VersioService service + +type VersioServiceServer interface { + Versions(context.Context, *VersionRequest) (*VersionsResponse, error) +} + +func RegisterVersioServiceServer(s *grpc.Server, srv VersioServiceServer) { + s.RegisterService(&_VersioService_serviceDesc, srv) +} + +func _VersioService_Versions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(VersionRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(VersioServiceServer).Versions(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/matrixProtos.VersioService/Versions", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(VersioServiceServer).Versions(ctx, req.(*VersionRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _VersioService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "matrixProtos.VersioService", + HandlerType: (*VersioServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Versions", + Handler: _VersioService_Versions_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "matrix.proto", +} + // Client API for LoginService service type LoginServiceClient interface { @@ -253,25 +335,28 @@ var _LoginService_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("matrix.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 319 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xcd, 0x4e, 0xc2, 0x40, - 0x10, 0x80, 0x53, 0x81, 0x16, 0x87, 0x92, 0x98, 0x8d, 0x3f, 0x1b, 0x3c, 0x88, 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 0x07, 0xde, 0x3d, 0x5a, 0xa5, 0x2e, 0x60, 0xb0, 0x32, 0x15, 0x2e, 0x19, - 0x69, 0xbb, 0xab, 0x03, 0xab, 0x1e, 0x9c, 0x51, 0x67, 0x10, 0xd9, 0xd6, 0x65, 0xd9, 0x26, 0x86, - 0x16, 0xef, 0x0a, 0x35, 0x81, 0x21, 0xe1, 0x0b, 0x21, 0xaf, 0xe4, 0x74, 0xdf, 0x19, 0x8b, 0xf4, - 0xc7, 0x6b, 0x88, 0x90, 0x28, 0x37, 0x05, 0xb6, 0xb5, 0x82, 0xea, 0x18, 0x7a, 0x48, 0x64, 0x48, - 0x87, 0xce, 0x7b, 0x98, 0x2f, 0xe4, 0x26, 0xed, 0xc3, 0xcb, 0x1c, 0xd5, 0x0d, 0xf4, 0x1c, 0xab, - 0xd1, 0xdf, 0x2f, 0xb5, 0x7f, 0xdd, 0xa3, 0xf3, 0x7f, 0xe7, 0xfc, 0x1d, 0x3c, 0x87, 0xee, 0xef, - 0xb8, 0xfe, 0x0d, 0x00, 0x00, 0xff, 0xff, 0x37, 0x21, 0x76, 0xf7, 0x2d, 0x02, 0x00, 0x00, + // 357 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x4f, 0x4b, 0xf3, 0x40, + 0x10, 0xc6, 0xc9, 0xdb, 0x36, 0xe9, 0x3b, 0x4d, 0x5f, 0x5e, 0x16, 0xff, 0x2c, 0x51, 0xb4, 0xa6, + 0x97, 0xe2, 0xa1, 0x60, 0xfd, 0x02, 0x5e, 0x04, 0x05, 0x0f, 0xa5, 0x8a, 0xe0, 0xa9, 0xc4, 0x64, + 0xb4, 0x41, 0xd2, 0x8d, 0x3b, 0x69, 0xd5, 0xbb, 0x5f, 0xcc, 0x6f, 0x26, 0x9b, 0x9d, 0x0d, 0x2d, + 0xf4, 0xb6, 0xcf, 0x6f, 0x9f, 0x99, 0x3c, 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, 0x8d, 0xbc, + 0xcd, 0xc4, 0x10, 0xfa, 0x1a, 0x5f, 0x34, 0xd2, 0x82, 0xbb, 0xdb, 0x9c, 0x21, 0x43, 0xdb, 0x5e, + 0x42, 0x80, 0x5a, 0xa7, 0x2a, 0x43, 0x97, 0x96, 0xa5, 0xd8, 0x83, 0x0e, 0x6a, 0xad, 0xb4, 0xf4, + 0x6b, 0x6e, 0xc5, 0xe4, 0x09, 0xfa, 0x76, 0xec, 0xe6, 0xeb, 0x79, 0x8a, 0xe2, 0x06, 0xba, 0x6e, + 0xe7, 0xe2, 0x78, 0xf7, 0x66, 0xed, 0xd0, 0xa3, 0x93, 0x9d, 0xb7, 0xcd, 0x4b, 0x99, 0x4c, 0x79, + 0x49, 0xae, 0xf3, 0x15, 0x74, 0x6a, 0x2d, 0xa2, 0xed, 0xc2, 0xcd, 0x4d, 0x46, 0x47, 0x3b, 0xef, + 0x6c, 0xc7, 0x67, 0xbf, 0x7e, 0xa4, 0x97, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x21, 0xbc, 0x71, + 0x89, 0xb4, 0x02, 0x00, 0x00, } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto b/src/github.com/Nordgedanken/MatrixProtoBuf/matrixProtos/matrix.proto @@ -1,6 +1,10 @@ syntax = "proto3"; package matrixProtos; +service VersioService { + rpc Versions (VersionRequest) returns (VersionsResponse); +} + message VersionsResponse { repeated Version versions = 1; } @@ -9,6 +13,10 @@ message Version { string version = 1; } +message VersionRequest { + string address = 1; +} + service LoginService { rpc Login (LoginRequest) returns (LoginResponse); } diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go @@ -1,9 +1,12 @@ package protobufS import ( + "encoding/json" "fmt" + "github.com/Nordgedanken/MatrixProtoBuf/jsonTypes" pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos" "github.com/golang/protobuf/proto" + "io/ioutil" "log" "net/http" "os" @@ -13,13 +16,37 @@ func StartProtoServer() { log.Println("Started ProtoBuf Server") http.HandleFunc("/versions", func(w http.ResponseWriter, r *http.Request) { + var versionRequest pb.VersionRequest + var hsVersions jsonTypes.HSVersions + + req, err := ioutil.ReadAll(r.Body) + + if err := proto.Unmarshal(req, &versionRequest); err != nil { + fmt.Println(err) + } + + reqresp, err := http.Get("https://" + versionRequest.Address + "/_matrix/client/versions") + if err != nil { + panic(err) + } + defer reqresp.Body.Close() + body, err := ioutil.ReadAll(reqresp.Body) + + if err = json.Unmarshal(body, &hsVersions); err != nil { + fmt.Println(err) + } + + fmt.Println(hsVersions) + resp := &pb.VersionsResponse{ - Versions: []*pb.Version{ - {"r0.0.1"}, - {"r0.1.0"}, - {"r0.2.0"}, - }, + Versions: []*pb.Version{}, } + + for _, k := range hsVersions.Versions { + fmt.Println(k) + resp.Versions = append(resp.Versions, &pb.Version{Version: k}) + } + out, err := proto.Marshal(resp) checkError(err) diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/rpc/rpcServer.go @@ -13,11 +13,11 @@ const ( port = ":50051" ) -// server is used to implement helloworld.GreeterServer. -type server struct{} +// server is used to implement helloworld.LoginServiceServer. +type loginServ struct{} -// SayHello implements helloworld.GreeterServer -func (s *server) Login(_ context.Context, r *pb.LoginRequest) (*pb.LoginResponse, error) { +// Login implements helloworld.LoginServiceServer +func (s *loginServ) Login(_ context.Context, r *pb.LoginRequest) (*pb.LoginResponse, error) { return &pb.LoginResponse{"test", "localhost", r.User, "test", "", ""}, nil } @@ -28,7 +28,7 @@ func StartRPCServer() { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() - pb.RegisterLoginServiceServer(s, &server{}) + pb.RegisterLoginServiceServer(s, &loginServ{}) // Register reflection service on gRPC server. reflection.Register(s) if err := s.Serve(lis); err != nil { diff --git a/src/github.com/Nordgedanken/mpbProtoClient/client.go b/src/github.com/Nordgedanken/mpbProtoClient/client.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos" "github.com/golang/protobuf/proto" @@ -15,7 +16,15 @@ func main() { func getVersions() { var versions pb.VersionsResponse - resp, err := http.Get("http://localhost:3000/versions") + VersionRequest := pb.VersionRequest{Address: "matrix.org"} + + data, err := proto.Marshal(&VersionRequest) + if err != nil { + fmt.Println(err) + return + } + + resp, err := http.Post("http://localhost:3000/versions", "", bytes.NewBuffer(data)) if err != nil { panic(err) }