From 7bbd58d0be8d2061a75f856e30d1006c8df6efe9 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 15 Sep 2021 13:59:31 +0100 Subject: [PATCH] Twitter API (#207) * add twitter service * update readme --- go.mod | 2 + go.sum | 6 + twitter/.gitignore | 2 + twitter/Dockerfile | 3 + twitter/Makefile | 28 +++ twitter/README.md | 6 + twitter/examples.json | 20 ++ twitter/generate.go | 3 + twitter/handler/twitter.go | 84 ++++++++ twitter/main.go | 24 +++ twitter/micro.mu | 1 + twitter/proto/twitter.pb.go | 346 ++++++++++++++++++++++++++++++ twitter/proto/twitter.pb.micro.go | 93 ++++++++ twitter/proto/twitter.proto | 37 ++++ twitter/publicapi.json | 5 + 15 files changed, 660 insertions(+) create mode 100644 twitter/.gitignore create mode 100644 twitter/Dockerfile create mode 100644 twitter/Makefile create mode 100644 twitter/README.md create mode 100644 twitter/examples.json create mode 100644 twitter/generate.go create mode 100644 twitter/handler/twitter.go create mode 100644 twitter/main.go create mode 100644 twitter/micro.mu create mode 100644 twitter/proto/twitter.pb.go create mode 100644 twitter/proto/twitter.pb.micro.go create mode 100644 twitter/proto/twitter.proto create mode 100644 twitter/publicapi.json diff --git a/go.mod b/go.mod index 44c5fcb..df4acda 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 github.com/crufter/lexer v0.0.0-20120907053443-23fe8c7add01 github.com/crufter/nested v0.0.0-20210903145606-dea42c476b37 + github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e github.com/disintegration/imaging v1.6.2 github.com/enescakir/emoji v1.0.0 github.com/fatih/camelcase v1.0.0 @@ -47,6 +48,7 @@ require ( go.opencensus.io v0.22.4 // indirect golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 + golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 // indirect google.golang.org/genproto v0.0.0-20201001141541-efaab9d3c4f7 // indirect diff --git a/go.sum b/go.sum index 78b190b..e9991bb 100644 --- a/go.sum +++ b/go.sum @@ -67,6 +67,8 @@ github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGw github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o= github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 h1:6dGQY3apkf7lG3a1UFhS6grlo009buPFVy79RvNVUF4= github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10/go.mod h1:JWoVf4GJxCxM3iCiZSVoXNMV+JFG49L+ou70KK3HTvQ= +github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY= +github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU= github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -99,6 +101,10 @@ github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I= github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE= +github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e h1:o0sI/cfhAXdtAbiIVeTd4hmwtwgs4cQFwRBhmbx8AKY= +github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE= +github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU= +github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= diff --git a/twitter/.gitignore b/twitter/.gitignore new file mode 100644 index 0000000..7c6ce7c --- /dev/null +++ b/twitter/.gitignore @@ -0,0 +1,2 @@ + +twitter diff --git a/twitter/Dockerfile b/twitter/Dockerfile new file mode 100644 index 0000000..b77dc0f --- /dev/null +++ b/twitter/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD twitter /twitter +ENTRYPOINT [ "/twitter" ] diff --git a/twitter/Makefile b/twitter/Makefile new file mode 100644 index 0000000..99cc49e --- /dev/null +++ b/twitter/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/twitter.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/twitter.proto + +.PHONY: build +build: + go build -o twitter *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t twitter:latest diff --git a/twitter/README.md b/twitter/README.md new file mode 100644 index 0000000..cb88ede --- /dev/null +++ b/twitter/README.md @@ -0,0 +1,6 @@ +Simple twitter timeline + +# Twitter Service + +Retrieve the twitter timeline for a user without all the extra baggage + diff --git a/twitter/examples.json b/twitter/examples.json new file mode 100644 index 0000000..b30817e --- /dev/null +++ b/twitter/examples.json @@ -0,0 +1,20 @@ +{ + "timeline": [{ + "title": "Get a twitter timeline", + "run_check": false, + "request": { + "username": "m3oservices", + "limit": 1 + }, + "response": { + "tweets": [{ + "id": "1437415171966873611", + "text": "Leveraging Micro APIs through Nocode (using @bubble) https://t.co/I5Z6bx31c7 #bubble #nocode #microapis #publicapis #m3o", + "username": "m3oservices", + "created_at": "Mon Sep 13 13:57:37 +0000 2021", + "retweeted_count": "2", + "favourited_count": "2" + }] + } + }] +} diff --git a/twitter/generate.go b/twitter/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/twitter/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/twitter/handler/twitter.go b/twitter/handler/twitter.go new file mode 100644 index 0000000..85d27bb --- /dev/null +++ b/twitter/handler/twitter.go @@ -0,0 +1,84 @@ +package handler + +import ( + "context" + + "github.com/dghubble/go-twitter/twitter" + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/logger" + pb "github.com/micro/services/twitter/proto" + "golang.org/x/oauth2" + "golang.org/x/oauth2/clientcredentials" +) + +type Twitter struct { + Client *twitter.Client +} + +func New() *Twitter { + v, err := config.Get("twitter.api_key") + if err != nil { + logger.Fatalf("twitter.api_key config not found: %v", err) + } + apiKey := v.String("") + if len(apiKey) == 0 { + logger.Fatal("twitter.api_key config not found") + } + v, err = config.Get("twitter.api_secret") + if err != nil { + logger.Fatalf("twitter.api_secret config not found: %v", err) + } + apiSecret := v.String("") + if len(apiSecret) == 0 { + logger.Fatal("twitter.api_secret config not found") + } + + // oauth2 configures a client that uses app credentials to keep a fresh token + config := &clientcredentials.Config{ + ClientID: apiKey, + ClientSecret: apiSecret, + TokenURL: "https://api.twitter.com/oauth2/token", + } + // http.Client will automatically authorize Requests + httpClient := config.Client(oauth2.NoContext) + + // Twitter client + client := twitter.NewClient(httpClient) + + return &Twitter{ + Client: client, + } +} + +func (t *Twitter) Timeline(ctx context.Context, req *pb.TimelineRequest, rsp *pb.TimelineResponse) error { + if len(req.Username) == 0 { + return errors.BadRequest("twitter.timeline", "missing username") + } + + if req.Limit <= 0 { + req.Limit = 20 + } + + tweets, _, err := t.Client.Timelines.UserTimeline(&twitter.UserTimelineParams{ + ScreenName: req.Username, + Count: int(req.Limit), + }) + if err != nil { + logger.Errorf("Failed to retrieve tweets for %v: %v", req.Username, err) + return errors.InternalServerError("twitter.timeline", "Failed to retrieve tweets for %v: %v", req.Username, err) + } + + for _, tweet := range tweets { + rsp.Tweets = append(rsp.Tweets, &pb.Tweet{ + Id: tweet.ID, + Text: tweet.Text, + CreatedAt: tweet.CreatedAt, + FavouritedCount: int64(tweet.FavoriteCount), + RetweetedCount: int64(tweet.RetweetCount), + Username: tweet.User.ScreenName, + }) + } + + return nil +} diff --git a/twitter/main.go b/twitter/main.go new file mode 100644 index 0000000..da4bc8c --- /dev/null +++ b/twitter/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" + "github.com/micro/services/twitter/handler" + pb "github.com/micro/services/twitter/proto" +) + +func main() { + // Create service + srv := service.New( + service.Name("twitter"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterTwitterHandler(srv.Server(), handler.New()) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/twitter/micro.mu b/twitter/micro.mu new file mode 100644 index 0000000..698d2bd --- /dev/null +++ b/twitter/micro.mu @@ -0,0 +1 @@ +service twitter diff --git a/twitter/proto/twitter.pb.go b/twitter/proto/twitter.pb.go new file mode 100644 index 0000000..137f126 --- /dev/null +++ b/twitter/proto/twitter.pb.go @@ -0,0 +1,346 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: proto/twitter.proto + +package twitter + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Tweet struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // id of the tweet + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + // text of the tweet + Text string `protobuf:"bytes,2,opt,name=text,proto3" json:"text,omitempty"` + // username of the person who tweeted + Username string `protobuf:"bytes,3,opt,name=username,proto3" json:"username,omitempty"` + // time of tweet + CreatedAt string `protobuf:"bytes,4,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` + // number of times retweeted + RetweetedCount int64 `protobuf:"varint,5,opt,name=retweeted_count,json=retweetedCount,proto3" json:"retweeted_count,omitempty"` + // number of times favourited + FavouritedCount int64 `protobuf:"varint,6,opt,name=favourited_count,json=favouritedCount,proto3" json:"favourited_count,omitempty"` +} + +func (x *Tweet) Reset() { + *x = Tweet{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_twitter_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Tweet) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Tweet) ProtoMessage() {} + +func (x *Tweet) ProtoReflect() protoreflect.Message { + mi := &file_proto_twitter_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Tweet.ProtoReflect.Descriptor instead. +func (*Tweet) Descriptor() ([]byte, []int) { + return file_proto_twitter_proto_rawDescGZIP(), []int{0} +} + +func (x *Tweet) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Tweet) GetText() string { + if x != nil { + return x.Text + } + return "" +} + +func (x *Tweet) GetUsername() string { + if x != nil { + return x.Username + } + return "" +} + +func (x *Tweet) GetCreatedAt() string { + if x != nil { + return x.CreatedAt + } + return "" +} + +func (x *Tweet) GetRetweetedCount() int64 { + if x != nil { + return x.RetweetedCount + } + return 0 +} + +func (x *Tweet) GetFavouritedCount() int64 { + if x != nil { + return x.FavouritedCount + } + return 0 +} + +// Get the timeline for a given user +type TimelineRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the username to request the timeline for + Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"` + // number of tweets to return. default: 20 + Limit int32 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` +} + +func (x *TimelineRequest) Reset() { + *x = TimelineRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_twitter_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *TimelineRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*TimelineRequest) ProtoMessage() {} + +func (x *TimelineRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_twitter_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use TimelineRequest.ProtoReflect.Descriptor instead. +func (*TimelineRequest) Descriptor() ([]byte, []int) { + return file_proto_twitter_proto_rawDescGZIP(), []int{1} +} + +func (x *TimelineRequest) GetUsername() string { + if x != nil { + return x.Username + } + return "" +} + +func (x *TimelineRequest) GetLimit() int32 { + if x != nil { + return x.Limit + } + return 0 +} + +type TimelineResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // The recent tweets for the user + Tweets []*Tweet `protobuf:"bytes,1,rep,name=tweets,proto3" json:"tweets,omitempty"` +} + +func (x *TimelineResponse) Reset() { + *x = TimelineResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_twitter_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *TimelineResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*TimelineResponse) ProtoMessage() {} + +func (x *TimelineResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_twitter_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use TimelineResponse.ProtoReflect.Descriptor instead. +func (*TimelineResponse) Descriptor() ([]byte, []int) { + return file_proto_twitter_proto_rawDescGZIP(), []int{2} +} + +func (x *TimelineResponse) GetTweets() []*Tweet { + if x != nil { + return x.Tweets + } + return nil +} + +var File_proto_twitter_proto protoreflect.FileDescriptor + +var file_proto_twitter_proto_rawDesc = []byte{ + 0x0a, 0x13, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x22, 0xba, + 0x01, 0x0a, 0x05, 0x54, 0x77, 0x65, 0x65, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x1a, 0x0a, 0x08, + 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, + 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x27, 0x0a, 0x0f, 0x72, 0x65, 0x74, 0x77, 0x65, + 0x65, 0x74, 0x65, 0x64, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x0e, 0x72, 0x65, 0x74, 0x77, 0x65, 0x65, 0x74, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, + 0x12, 0x29, 0x0a, 0x10, 0x66, 0x61, 0x76, 0x6f, 0x75, 0x72, 0x69, 0x74, 0x65, 0x64, 0x5f, 0x63, + 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0f, 0x66, 0x61, 0x76, 0x6f, + 0x75, 0x72, 0x69, 0x74, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x43, 0x0a, 0x0f, 0x54, + 0x69, 0x6d, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, + 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, + 0x6d, 0x69, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, + 0x22, 0x3a, 0x0a, 0x10, 0x54, 0x69, 0x6d, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x26, 0x0a, 0x06, 0x74, 0x77, 0x65, 0x65, 0x74, 0x73, 0x18, 0x01, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x2e, 0x54, + 0x77, 0x65, 0x65, 0x74, 0x52, 0x06, 0x74, 0x77, 0x65, 0x65, 0x74, 0x73, 0x32, 0x4c, 0x0a, 0x07, + 0x54, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x12, 0x41, 0x0a, 0x08, 0x54, 0x69, 0x6d, 0x65, 0x6c, + 0x69, 0x6e, 0x65, 0x12, 0x18, 0x2e, 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x2e, 0x54, 0x69, + 0x6d, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, + 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x6c, 0x69, 0x6e, 0x65, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x74, 0x77, 0x69, 0x74, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_twitter_proto_rawDescOnce sync.Once + file_proto_twitter_proto_rawDescData = file_proto_twitter_proto_rawDesc +) + +func file_proto_twitter_proto_rawDescGZIP() []byte { + file_proto_twitter_proto_rawDescOnce.Do(func() { + file_proto_twitter_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_twitter_proto_rawDescData) + }) + return file_proto_twitter_proto_rawDescData +} + +var file_proto_twitter_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_twitter_proto_goTypes = []interface{}{ + (*Tweet)(nil), // 0: twitter.Tweet + (*TimelineRequest)(nil), // 1: twitter.TimelineRequest + (*TimelineResponse)(nil), // 2: twitter.TimelineResponse +} +var file_proto_twitter_proto_depIdxs = []int32{ + 0, // 0: twitter.TimelineResponse.tweets:type_name -> twitter.Tweet + 1, // 1: twitter.Twitter.Timeline:input_type -> twitter.TimelineRequest + 2, // 2: twitter.Twitter.Timeline:output_type -> twitter.TimelineResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_proto_twitter_proto_init() } +func file_proto_twitter_proto_init() { + if File_proto_twitter_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_twitter_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Tweet); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_twitter_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*TimelineRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_twitter_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*TimelineResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_twitter_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_twitter_proto_goTypes, + DependencyIndexes: file_proto_twitter_proto_depIdxs, + MessageInfos: file_proto_twitter_proto_msgTypes, + }.Build() + File_proto_twitter_proto = out.File + file_proto_twitter_proto_rawDesc = nil + file_proto_twitter_proto_goTypes = nil + file_proto_twitter_proto_depIdxs = nil +} diff --git a/twitter/proto/twitter.pb.micro.go b/twitter/proto/twitter.pb.micro.go new file mode 100644 index 0000000..0af7f40 --- /dev/null +++ b/twitter/proto/twitter.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/twitter.proto + +package twitter + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Twitter service + +func NewTwitterEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Twitter service + +type TwitterService interface { + Timeline(ctx context.Context, in *TimelineRequest, opts ...client.CallOption) (*TimelineResponse, error) +} + +type twitterService struct { + c client.Client + name string +} + +func NewTwitterService(name string, c client.Client) TwitterService { + return &twitterService{ + c: c, + name: name, + } +} + +func (c *twitterService) Timeline(ctx context.Context, in *TimelineRequest, opts ...client.CallOption) (*TimelineResponse, error) { + req := c.c.NewRequest(c.name, "Twitter.Timeline", in) + out := new(TimelineResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Twitter service + +type TwitterHandler interface { + Timeline(context.Context, *TimelineRequest, *TimelineResponse) error +} + +func RegisterTwitterHandler(s server.Server, hdlr TwitterHandler, opts ...server.HandlerOption) error { + type twitter interface { + Timeline(ctx context.Context, in *TimelineRequest, out *TimelineResponse) error + } + type Twitter struct { + twitter + } + h := &twitterHandler{hdlr} + return s.Handle(s.NewHandler(&Twitter{h}, opts...)) +} + +type twitterHandler struct { + TwitterHandler +} + +func (h *twitterHandler) Timeline(ctx context.Context, in *TimelineRequest, out *TimelineResponse) error { + return h.TwitterHandler.Timeline(ctx, in, out) +} diff --git a/twitter/proto/twitter.proto b/twitter/proto/twitter.proto new file mode 100644 index 0000000..584b1ca --- /dev/null +++ b/twitter/proto/twitter.proto @@ -0,0 +1,37 @@ +syntax = "proto3"; + +package twitter; + +option go_package = "./proto;twitter"; + +service Twitter { + rpc Timeline(TimelineRequest) returns (TimelineResponse) {} +} + +message Tweet { + // id of the tweet + int64 id = 1; + // text of the tweet + string text = 2; + // username of the person who tweeted + string username = 3; + // time of tweet + string created_at = 4; + // number of times retweeted + int64 retweeted_count = 5; + // number of times favourited + int64 favourited_count = 6; +} + +// Get the timeline for a given user +message TimelineRequest { + // the username to request the timeline for + string username = 1; + // number of tweets to return. default: 20 + int32 limit = 2; +} + +message TimelineResponse { + // The recent tweets for the user + repeated Tweet tweets = 1; +} diff --git a/twitter/publicapi.json b/twitter/publicapi.json new file mode 100644 index 0000000..38691e2 --- /dev/null +++ b/twitter/publicapi.json @@ -0,0 +1,5 @@ +{ + "name": "twitter", + "icon": "💬", + "category": "social" +}