From 9e0be6c85d7516051518feaaf1d2ffb538323918 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 18 Jan 2022 15:27:36 +0000 Subject: [PATCH] add service updates (#2418) --- services/cache/cache.go | 16 ++++++ services/carbon/carbon.go | 52 +++++++++++++++++++ services/search/search.go | 103 ++++++++++++++++++++++++++++++++++---- services/services.go | 3 ++ services/update.sh | 24 +++++++++ 5 files changed, 187 insertions(+), 11 deletions(-) create mode 100755 services/carbon/carbon.go create mode 100755 services/update.sh diff --git a/services/cache/cache.go b/services/cache/cache.go index 3036b052..758b3a16 100755 --- a/services/cache/cache.go +++ b/services/cache/cache.go @@ -9,6 +9,7 @@ type Cache interface { Delete(*DeleteRequest) (*DeleteResponse, error) Get(*GetRequest) (*GetResponse, error) Increment(*IncrementRequest) (*IncrementResponse, error) + ListKeys(*ListKeysRequest) (*ListKeysResponse, error) Set(*SetRequest) (*SetResponse, error) } @@ -56,6 +57,14 @@ func (t *CacheService) Increment(request *IncrementRequest) (*IncrementResponse, } +// List all the available keys +func (t *CacheService) ListKeys(request *ListKeysRequest) (*ListKeysResponse, error) { + + rsp := &ListKeysResponse{} + return rsp, t.client.Call("cache", "ListKeys", request, rsp) + +} + // Set an item in the cache. Overwrites any existing value already set. func (t *CacheService) Set(request *SetRequest) (*SetResponse, error) { @@ -116,6 +125,13 @@ type IncrementResponse struct { Value int64 `json:"value,string"` } +type ListKeysRequest struct { +} + +type ListKeysResponse struct { + Keys []string `json:"keys"` +} + type SetRequest struct { // The key to update Key string `json:"key"` diff --git a/services/carbon/carbon.go b/services/carbon/carbon.go new file mode 100755 index 00000000..4ad1a18c --- /dev/null +++ b/services/carbon/carbon.go @@ -0,0 +1,52 @@ +package carbon + +import ( + "go-micro.dev/v4/api/client" +) + +type Carbon interface { + Offset(*OffsetRequest) (*OffsetResponse, error) +} + +func NewCarbonService(token string) *CarbonService { + return &CarbonService{ + client: client.NewClient(&client.Options{ + Token: token, + }), + } +} + +type CarbonService struct { + client *client.Client +} + +// Purchase 1KG (0.001 tonne) of carbon offsets in a single request +func (t *CarbonService) Offset(request *OffsetRequest) (*OffsetResponse, error) { + + rsp := &OffsetResponse{} + return rsp, t.client.Call("carbon", "Offset", request, rsp) + +} + +type OffsetRequest struct { +} + +type OffsetResponse struct { + // the metric used e.g KG or Tonnes + Metric string `json:"metric"` + // projects it was allocated to + Projects []Project `json:"projects"` + // number of tonnes + Tonnes float64 `json:"tonnes"` + // number of units purchased + Units int32 `json:"units"` +} + +type Project struct { + // name of the project + Name string `json:"name"` + // percentage that went to this + Percentage float64 `json:"percentage"` + // amount in tonnes + Tonnes float64 `json:"tonnes"` +} diff --git a/services/search/search.go b/services/search/search.go index 2a897f8d..56e9b592 100755 --- a/services/search/search.go +++ b/services/search/search.go @@ -5,7 +5,10 @@ import ( ) type Search interface { - Vote(*VoteRequest) (*VoteResponse, error) + DeleteIndex(*DeleteIndexRequest) (*DeleteIndexResponse, error) + Delete(*DeleteRequest) (*DeleteResponse, error) + Index(*IndexRequest) (*IndexResponse, error) + Search(*SearchRequest) (*SearchResponse, error) } func NewSearchService(token string) *SearchService { @@ -20,20 +23,98 @@ type SearchService struct { client *client.Client } -// Vote to have the Search api launched faster! -func (t *SearchService) Vote(request *VoteRequest) (*VoteResponse, error) { +// Delete an index. +func (t *SearchService) DeleteIndex(request *DeleteIndexRequest) (*DeleteIndexResponse, error) { - rsp := &VoteResponse{} - return rsp, t.client.Call("search", "Vote", request, rsp) + rsp := &DeleteIndexResponse{} + return rsp, t.client.Call("search", "DeleteIndex", request, rsp) } -type VoteRequest struct { - // optional message - Message string `json:"message"` +// Delete a document given its ID +func (t *SearchService) Delete(request *DeleteRequest) (*DeleteResponse, error) { + + rsp := &DeleteResponse{} + return rsp, t.client.Call("search", "Delete", request, rsp) + } -type VoteResponse struct { - // response message - Message string `json:"message"` +// Index a document i.e. insert a document to search for. +func (t *SearchService) Index(request *IndexRequest) (*IndexResponse, error) { + + rsp := &IndexResponse{} + return rsp, t.client.Call("search", "Index", request, rsp) + +} + +// Search for documents in a given in index +func (t *SearchService) Search(request *SearchRequest) (*SearchResponse, error) { + + rsp := &SearchResponse{} + return rsp, t.client.Call("search", "Search", request, rsp) + +} + +type CreateIndexRequest struct { + Fields []Field `json:"fields"` + // the name of the index + Index string `json:"index"` +} + +type CreateIndexResponse struct { +} + +type DeleteIndexRequest struct { + // The name of the index to delete + Index string `json:"index"` +} + +type DeleteIndexResponse struct { +} + +type DeleteRequest struct { + // The ID of the document to delete + Id string `json:"id"` + // The index the document belongs to + Index string `json:"index"` +} + +type DeleteResponse struct { +} + +type Document struct { + // The JSON contents of the document + Contents map[string]interface{} `json:"contents"` + // The ID for this document. If blank, one will be generated + Id string `json:"id"` +} + +type Field struct { + // The name of the field. Use a `.` separator to define nested fields e.g. foo.bar + Name string `json:"name"` + // The type of the field - string, number + Type string `json:"type"` +} + +type IndexRequest struct { + // The document to index + Document *Document `json:"document"` + // The index this document belongs to + Index string `json:"index"` +} + +type IndexResponse struct { + Id string `json:"id"` +} + +type SearchRequest struct { + // The index the document belongs to + Index string `json:"index"` + // The query. See docs for query language examples + Query string `json:"query"` +} + +type SearchResponse struct { + // The matching documents + Documents []Document `json:"documents"` } diff --git a/services/services.go b/services/services.go index 10b2a370..f79af55c 100755 --- a/services/services.go +++ b/services/services.go @@ -6,6 +6,7 @@ import ( "go-micro.dev/v4/services/app" "go-micro.dev/v4/services/avatar" "go-micro.dev/v4/services/cache" + "go-micro.dev/v4/services/carbon" "go-micro.dev/v4/services/contact" "go-micro.dev/v4/services/crypto" "go-micro.dev/v4/services/currency" @@ -67,6 +68,7 @@ func NewClient(token string) *Client { AppService: app.NewAppService(token), AvatarService: avatar.NewAvatarService(token), CacheService: cache.NewCacheService(token), + CarbonService: carbon.NewCarbonService(token), ContactService: contact.NewContactService(token), CryptoService: crypto.NewCryptoService(token), CurrencyService: currency.NewCurrencyService(token), @@ -128,6 +130,7 @@ type Client struct { AppService *app.AppService AvatarService *avatar.AvatarService CacheService *cache.CacheService + CarbonService *carbon.CarbonService ContactService *contact.ContactService CryptoService *crypto.CryptoService CurrencyService *currency.CurrencyService diff --git a/services/update.sh b/services/update.sh new file mode 100755 index 00000000..979082f4 --- /dev/null +++ b/services/update.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +DIR=/tmp/services +CDIR=`pwd` +REPO=https://github.com/m3o/m3o-go + +git clone $REPO $DIR +cd $DIR + +# move/delete +rm -rf .git .github go.mod go.sum README.md CNAME Makefile TODO.md LICENSE client cmd examples +mv m3o.go services.go + +# rewrite +grep -r "go.m3o.com" | cut -f 1 -d : | xargs sed -i 's@go.m3o.com/client@go-micro.dev/v4/api/client@g' +sed -i 's@go.m3o.com@go-micro.dev/v4/services@g' services.go +sed -i 's@package m3o@package services@g' services.go + +# sync +cd $CDIR +rsync -avz $DIR/ . + +# cleanup +rm -rf $DIR