Skip to content

Commit

Permalink
add service updates (#2418)
Browse files Browse the repository at this point in the history
  • Loading branch information
asim authored Jan 18, 2022
1 parent 23f1de8 commit 9e0be6c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 11 deletions.
16 changes: 16 additions & 0 deletions services/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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"`
Expand Down
52 changes: 52 additions & 0 deletions services/carbon/carbon.go
Original file line number Diff line number Diff line change
@@ -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"`
}
103 changes: 92 additions & 11 deletions services/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

}

// 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 VoteResponse struct {
// response message
Message string `json:"message"`
type SearchResponse struct {
// The matching documents
Documents []Document `json:"documents"`
}
3 changes: 3 additions & 0 deletions services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions services/update.sh
Original file line number Diff line number Diff line change
@@ -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 '[email protected]/[email protected]/v4/api/client@g'
sed -i '[email protected]@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

0 comments on commit 9e0be6c

Please sign in to comment.