mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
add notes (#218)
* add notes * fix build * Commit from GitHub Actions (Publish APIs & Clients) Co-authored-by: asim <asim@users.noreply.github.com>
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/micro/services/clients/go/image"
|
"github.com/micro/services/clients/go/image"
|
||||||
"github.com/micro/services/clients/go/ip"
|
"github.com/micro/services/clients/go/ip"
|
||||||
"github.com/micro/services/clients/go/location"
|
"github.com/micro/services/clients/go/location"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
"github.com/micro/services/clients/go/otp"
|
"github.com/micro/services/clients/go/otp"
|
||||||
"github.com/micro/services/clients/go/postcode"
|
"github.com/micro/services/clients/go/postcode"
|
||||||
"github.com/micro/services/clients/go/prayer"
|
"github.com/micro/services/clients/go/prayer"
|
||||||
@@ -59,6 +60,7 @@ func NewClient(token string) *Client {
|
|||||||
ImageService: image.NewImageService(token),
|
ImageService: image.NewImageService(token),
|
||||||
IpService: ip.NewIpService(token),
|
IpService: ip.NewIpService(token),
|
||||||
LocationService: location.NewLocationService(token),
|
LocationService: location.NewLocationService(token),
|
||||||
|
NotesService: notes.NewNotesService(token),
|
||||||
OtpService: otp.NewOtpService(token),
|
OtpService: otp.NewOtpService(token),
|
||||||
PostcodeService: postcode.NewPostcodeService(token),
|
PostcodeService: postcode.NewPostcodeService(token),
|
||||||
PrayerService: prayer.NewPrayerService(token),
|
PrayerService: prayer.NewPrayerService(token),
|
||||||
@@ -100,6 +102,7 @@ type Client struct {
|
|||||||
ImageService *image.ImageService
|
ImageService *image.ImageService
|
||||||
IpService *ip.IpService
|
IpService *ip.IpService
|
||||||
LocationService *location.LocationService
|
LocationService *location.LocationService
|
||||||
|
NotesService *notes.NotesService
|
||||||
OtpService *otp.OtpService
|
OtpService *otp.OtpService
|
||||||
PostcodeService *postcode.PostcodeService
|
PostcodeService *postcode.PostcodeService
|
||||||
PrayerService *prayer.PrayerService
|
PrayerService *prayer.PrayerService
|
||||||
|
|||||||
125
clients/go/notes/notes.go
Executable file
125
clients/go/notes/notes.go
Executable file
@@ -0,0 +1,125 @@
|
|||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/m3o/m3o-go/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewNotesService(token string) *NotesService {
|
||||||
|
return &NotesService{
|
||||||
|
client: client.NewClient(&client.Options{
|
||||||
|
Token: token,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type NotesService struct {
|
||||||
|
client *client.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new note
|
||||||
|
func (t *NotesService) Create(request *CreateRequest) (*CreateResponse, error) {
|
||||||
|
rsp := &CreateResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "Create", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a note
|
||||||
|
func (t *NotesService) Delete(request *DeleteRequest) (*DeleteResponse, error) {
|
||||||
|
rsp := &DeleteResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "Delete", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// List all the notes
|
||||||
|
func (t *NotesService) List(request *ListRequest) (*ListResponse, error) {
|
||||||
|
rsp := &ListResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "List", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a note
|
||||||
|
func (t *NotesService) Read(request *ReadRequest) (*ReadResponse, error) {
|
||||||
|
rsp := &ReadResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "Read", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specify the note to events
|
||||||
|
func (t *NotesService) Subscribe(request *SubscribeRequest) (*SubscribeResponse, error) {
|
||||||
|
rsp := &SubscribeResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "Subscribe", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update a note
|
||||||
|
func (t *NotesService) Update(request *UpdateRequest) (*UpdateResponse, error) {
|
||||||
|
rsp := &UpdateResponse{}
|
||||||
|
return rsp, t.client.Call("notes", "Update", request, rsp)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateRequest struct {
|
||||||
|
// note text
|
||||||
|
Text string `json:"text"`
|
||||||
|
// note title
|
||||||
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateResponse struct {
|
||||||
|
// The created note
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteRequest struct {
|
||||||
|
// specify the id of the note
|
||||||
|
Id string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteResponse struct {
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListRequest struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListResponse struct {
|
||||||
|
// the list of notes
|
||||||
|
Notes []Note `json:"notes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Note struct {
|
||||||
|
// time at which the note was created
|
||||||
|
Created string `json:"created"`
|
||||||
|
// unique id for the note, generated if not specified
|
||||||
|
Id string `json:"id"`
|
||||||
|
// text within the note
|
||||||
|
Text string `json:"text"`
|
||||||
|
// title of the note
|
||||||
|
Title string `json:"title"`
|
||||||
|
// time at which the note was updated
|
||||||
|
Updated string `json:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReadRequest struct {
|
||||||
|
// the note id
|
||||||
|
Id string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReadResponse struct {
|
||||||
|
// The note
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubscribeRequest struct {
|
||||||
|
// optionally specify a note id
|
||||||
|
Id string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubscribeResponse struct {
|
||||||
|
// the event which occured; created, deleted, updated
|
||||||
|
Event string `json:"event"`
|
||||||
|
// the note which the operation occured on
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateRequest struct {
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateResponse struct {
|
||||||
|
Note *Note `json:"note"`
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ import * as id from "./id";
|
|||||||
import * as image from "./image";
|
import * as image from "./image";
|
||||||
import * as ip from "./ip";
|
import * as ip from "./ip";
|
||||||
import * as location from "./location";
|
import * as location from "./location";
|
||||||
|
import * as notes from "./notes";
|
||||||
import * as otp from "./otp";
|
import * as otp from "./otp";
|
||||||
import * as postcode from "./postcode";
|
import * as postcode from "./postcode";
|
||||||
import * as prayer from "./prayer";
|
import * as prayer from "./prayer";
|
||||||
@@ -53,6 +54,7 @@ export class Client {
|
|||||||
this.imageService = new image.ImageService(token);
|
this.imageService = new image.ImageService(token);
|
||||||
this.ipService = new ip.IpService(token);
|
this.ipService = new ip.IpService(token);
|
||||||
this.locationService = new location.LocationService(token);
|
this.locationService = new location.LocationService(token);
|
||||||
|
this.notesService = new notes.NotesService(token);
|
||||||
this.otpService = new otp.OtpService(token);
|
this.otpService = new otp.OtpService(token);
|
||||||
this.postcodeService = new postcode.PostcodeService(token);
|
this.postcodeService = new postcode.PostcodeService(token);
|
||||||
this.prayerService = new prayer.PrayerService(token);
|
this.prayerService = new prayer.PrayerService(token);
|
||||||
@@ -90,6 +92,7 @@ export class Client {
|
|||||||
imageService: image.ImageService;
|
imageService: image.ImageService;
|
||||||
ipService: ip.IpService;
|
ipService: ip.IpService;
|
||||||
locationService: location.LocationService;
|
locationService: location.LocationService;
|
||||||
|
notesService: notes.NotesService;
|
||||||
otpService: otp.OtpService;
|
otpService: otp.OtpService;
|
||||||
postcodeService: postcode.PostcodeService;
|
postcodeService: postcode.PostcodeService;
|
||||||
prayerService: prayer.PrayerService;
|
prayerService: prayer.PrayerService;
|
||||||
|
|||||||
120
clients/ts/notes/index.ts
Executable file
120
clients/ts/notes/index.ts
Executable file
@@ -0,0 +1,120 @@
|
|||||||
|
import * as m3o from "@m3o/m3o-node";
|
||||||
|
|
||||||
|
export class NotesService {
|
||||||
|
private client: m3o.Client;
|
||||||
|
|
||||||
|
constructor(token: string) {
|
||||||
|
this.client = new m3o.Client({ token: token });
|
||||||
|
}
|
||||||
|
// Create a new note
|
||||||
|
create(request: CreateRequest): Promise<CreateResponse> {
|
||||||
|
return this.client.call(
|
||||||
|
"notes",
|
||||||
|
"Create",
|
||||||
|
request
|
||||||
|
) as Promise<CreateResponse>;
|
||||||
|
}
|
||||||
|
// Delete a note
|
||||||
|
delete(request: DeleteRequest): Promise<DeleteResponse> {
|
||||||
|
return this.client.call(
|
||||||
|
"notes",
|
||||||
|
"Delete",
|
||||||
|
request
|
||||||
|
) as Promise<DeleteResponse>;
|
||||||
|
}
|
||||||
|
// List all the notes
|
||||||
|
list(request: ListRequest): Promise<ListResponse> {
|
||||||
|
return this.client.call("notes", "List", request) as Promise<ListResponse>;
|
||||||
|
}
|
||||||
|
// Read a note
|
||||||
|
read(request: ReadRequest): Promise<ReadResponse> {
|
||||||
|
return this.client.call("notes", "Read", request) as Promise<ReadResponse>;
|
||||||
|
}
|
||||||
|
// Specify the note to events
|
||||||
|
subscribe(request: SubscribeRequest): Promise<SubscribeResponse> {
|
||||||
|
return this.client.call(
|
||||||
|
"notes",
|
||||||
|
"Subscribe",
|
||||||
|
request
|
||||||
|
) as Promise<SubscribeResponse>;
|
||||||
|
}
|
||||||
|
// Update a note
|
||||||
|
update(request: UpdateRequest): Promise<UpdateResponse> {
|
||||||
|
return this.client.call(
|
||||||
|
"notes",
|
||||||
|
"Update",
|
||||||
|
request
|
||||||
|
) as Promise<UpdateResponse>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateRequest {
|
||||||
|
// note text
|
||||||
|
text?: string;
|
||||||
|
// note title
|
||||||
|
title?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateResponse {
|
||||||
|
// The created note
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DeleteRequest {
|
||||||
|
// specify the id of the note
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DeleteResponse {
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ListRequest {}
|
||||||
|
|
||||||
|
export interface ListResponse {
|
||||||
|
// the list of notes
|
||||||
|
notes?: Note[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Note {
|
||||||
|
// time at which the note was created
|
||||||
|
created?: string;
|
||||||
|
// unique id for the note, generated if not specified
|
||||||
|
id?: string;
|
||||||
|
// text within the note
|
||||||
|
text?: string;
|
||||||
|
// title of the note
|
||||||
|
title?: string;
|
||||||
|
// time at which the note was updated
|
||||||
|
updated?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReadRequest {
|
||||||
|
// the note id
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReadResponse {
|
||||||
|
// The note
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SubscribeRequest {
|
||||||
|
// optionally specify a note id
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SubscribeResponse {
|
||||||
|
// the event which occured; created, deleted, updated
|
||||||
|
event?: string;
|
||||||
|
// the note which the operation occured on
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateRequest {
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateResponse {
|
||||||
|
note?: { [key: string]: any };
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
"./image": "./dist/image/index.js",
|
"./image": "./dist/image/index.js",
|
||||||
"./ip": "./dist/ip/index.js",
|
"./ip": "./dist/ip/index.js",
|
||||||
"./location": "./dist/location/index.js",
|
"./location": "./dist/location/index.js",
|
||||||
|
"./notes": "./dist/notes/index.js",
|
||||||
"./otp": "./dist/otp/index.js",
|
"./otp": "./dist/otp/index.js",
|
||||||
"./pkg": "./dist/pkg/index.js",
|
"./pkg": "./dist/pkg/index.js",
|
||||||
"./postcode": "./dist/postcode/index.js",
|
"./postcode": "./dist/postcode/index.js",
|
||||||
@@ -61,5 +62,5 @@
|
|||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"version": "1.0.530"
|
"version": "1.0.531"
|
||||||
}
|
}
|
||||||
7
examples/notes/create/curl/createANote.sh
Executable file
7
examples/notes/create/curl/createANote.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
curl "https://api.m3o.com/v1/notes/Create" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
||||||
|
-d '{
|
||||||
|
"text": "This is my note",
|
||||||
|
"title": "New Note"
|
||||||
|
}'
|
||||||
17
examples/notes/create/go/createANote.go
Executable file
17
examples/notes/create/go/createANote.go
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Create a new note
|
||||||
|
func CreateAnote() {
|
||||||
|
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
||||||
|
rsp, err := notesService.Create(¬es.CreateRequest{
|
||||||
|
Text: "This is my note",
|
||||||
|
Title: "New Note",
|
||||||
|
})
|
||||||
|
fmt.Println(rsp, err)
|
||||||
|
}
|
||||||
13
examples/notes/create/node/createANote.js
Executable file
13
examples/notes/create/node/createANote.js
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
import * as notes from "m3o/notes";
|
||||||
|
|
||||||
|
// Create a new note
|
||||||
|
async function CreateAnote() {
|
||||||
|
let notesService = new notes.NotesService(process.env.MICRO_API_TOKEN);
|
||||||
|
let rsp = await notesService.create({
|
||||||
|
text: "This is my note",
|
||||||
|
title: "New Note",
|
||||||
|
});
|
||||||
|
console.log(rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
await CreateAnote();
|
||||||
6
examples/notes/delete/curl/deleteANote.sh
Executable file
6
examples/notes/delete/curl/deleteANote.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
curl "https://api.m3o.com/v1/notes/Delete" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
||||||
|
-d '{
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
|
||||||
|
}'
|
||||||
16
examples/notes/delete/go/deleteANote.go
Executable file
16
examples/notes/delete/go/deleteANote.go
Executable file
@@ -0,0 +1,16 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Delete a note
|
||||||
|
func DeleteAnote() {
|
||||||
|
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
||||||
|
rsp, err := notesService.Delete(¬es.DeleteRequest{
|
||||||
|
Id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
})
|
||||||
|
fmt.Println(rsp, err)
|
||||||
|
}
|
||||||
12
examples/notes/delete/node/deleteANote.js
Executable file
12
examples/notes/delete/node/deleteANote.js
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
import * as notes from "m3o/notes";
|
||||||
|
|
||||||
|
// Delete a note
|
||||||
|
async function DeleteAnote() {
|
||||||
|
let notesService = new notes.NotesService(process.env.MICRO_API_TOKEN);
|
||||||
|
let rsp = await notesService.delete({
|
||||||
|
id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
});
|
||||||
|
console.log(rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
await DeleteAnote();
|
||||||
4
examples/notes/list/curl/listAllNotes.sh
Executable file
4
examples/notes/list/curl/listAllNotes.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
curl "https://api.m3o.com/v1/notes/List" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
||||||
|
-d '{}'
|
||||||
14
examples/notes/list/go/listAllNotes.go
Executable file
14
examples/notes/list/go/listAllNotes.go
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// List all the notes
|
||||||
|
func ListAllNotes() {
|
||||||
|
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
||||||
|
rsp, err := notesService.List(¬es.ListRequest{})
|
||||||
|
fmt.Println(rsp, err)
|
||||||
|
}
|
||||||
10
examples/notes/list/node/listAllNotes.js
Executable file
10
examples/notes/list/node/listAllNotes.js
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
import * as notes from "m3o/notes";
|
||||||
|
|
||||||
|
// List all the notes
|
||||||
|
async function ListAllNotes() {
|
||||||
|
let notesService = new notes.NotesService(process.env.MICRO_API_TOKEN);
|
||||||
|
let rsp = await notesService.list({});
|
||||||
|
console.log(rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ListAllNotes();
|
||||||
6
examples/notes/read/curl/readANote.sh
Executable file
6
examples/notes/read/curl/readANote.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
curl "https://api.m3o.com/v1/notes/Read" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
||||||
|
-d '{
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
|
||||||
|
}'
|
||||||
16
examples/notes/read/go/readANote.go
Executable file
16
examples/notes/read/go/readANote.go
Executable file
@@ -0,0 +1,16 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Read a note
|
||||||
|
func ReadAnote() {
|
||||||
|
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
||||||
|
rsp, err := notesService.Read(¬es.ReadRequest{
|
||||||
|
Id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
})
|
||||||
|
fmt.Println(rsp, err)
|
||||||
|
}
|
||||||
12
examples/notes/read/node/readANote.js
Executable file
12
examples/notes/read/node/readANote.js
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
import * as notes from "m3o/notes";
|
||||||
|
|
||||||
|
// Read a note
|
||||||
|
async function ReadAnote() {
|
||||||
|
let notesService = new notes.NotesService(process.env.MICRO_API_TOKEN);
|
||||||
|
let rsp = await notesService.read({
|
||||||
|
id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
});
|
||||||
|
console.log(rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ReadAnote();
|
||||||
10
examples/notes/update/curl/updateANote.sh
Executable file
10
examples/notes/update/curl/updateANote.sh
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
curl "https://api.m3o.com/v1/notes/Update" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
||||||
|
-d '{
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"text": "Updated note text",
|
||||||
|
"title": "Update Note"
|
||||||
|
}
|
||||||
|
}'
|
||||||
20
examples/notes/update/go/updateANote.go
Executable file
20
examples/notes/update/go/updateANote.go
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/micro/services/clients/go/notes"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update a note
|
||||||
|
func UpdateAnote() {
|
||||||
|
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
||||||
|
rsp, err := notesService.Update(¬es.UpdateRequest{
|
||||||
|
Note: ¬es.Note{
|
||||||
|
Id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
Text: "Updated note text",
|
||||||
|
Title: "Update Note",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
fmt.Println(rsp, err)
|
||||||
|
}
|
||||||
16
examples/notes/update/node/updateANote.js
Executable file
16
examples/notes/update/node/updateANote.js
Executable file
@@ -0,0 +1,16 @@
|
|||||||
|
import * as notes from "m3o/notes";
|
||||||
|
|
||||||
|
// Update a note
|
||||||
|
async function UpdateAnote() {
|
||||||
|
let notesService = new notes.NotesService(process.env.MICRO_API_TOKEN);
|
||||||
|
let rsp = await notesService.update({
|
||||||
|
note: {
|
||||||
|
id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
text: "Updated note text",
|
||||||
|
title: "Update Note",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
await UpdateAnote();
|
||||||
29
notes/Makefile
Normal file
29
notes/Makefile
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
GOPATH:=$(shell go env GOPATH)
|
||||||
|
|
||||||
|
.PHONY: api
|
||||||
|
api:
|
||||||
|
protoc --openapi_out=. --proto_path=. proto/notes.proto
|
||||||
|
|
||||||
|
.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: proto
|
||||||
|
proto:
|
||||||
|
protoc --proto_path=. --micro_out=. --go_out=:. proto/notes.proto
|
||||||
|
|
||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
go build -o notes *.go
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -v ./... -cover
|
||||||
|
|
||||||
|
.PHONY: docker
|
||||||
|
docker:
|
||||||
|
docker build . -t notes:latest
|
||||||
7
notes/README.md
Normal file
7
notes/README.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Store and retrieve notes
|
||||||
|
|
||||||
|
# Notes Service
|
||||||
|
|
||||||
|
The notes service lets you keep track of notes as first class objects. Subscribe to changes and
|
||||||
|
perform fast retrieval of all your notes.
|
||||||
|
|
||||||
93
notes/examples.json
Normal file
93
notes/examples.json
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
|
||||||
|
{
|
||||||
|
"create": [{
|
||||||
|
"title": "Create a note",
|
||||||
|
"description": "Create a simple text based note",
|
||||||
|
"run_check": true,
|
||||||
|
"request": {
|
||||||
|
"title": "New Note",
|
||||||
|
"text": "This is my note"
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"created": "2021-09-29T13:33:03+01:00",
|
||||||
|
"updated": "2021-09-29T13:33:03+01:00",
|
||||||
|
"title": "New Note",
|
||||||
|
"text": "This is my note"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"read": [{
|
||||||
|
"title": "Read a note",
|
||||||
|
"description": "Read a note by its ID",
|
||||||
|
"run_check": true,
|
||||||
|
"request": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"created": "2021-09-29T13:33:03+01:00",
|
||||||
|
"updated": "2021-09-29T13:33:03+01:00",
|
||||||
|
"title": "New Note",
|
||||||
|
"text": "This is my note"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"list": [{
|
||||||
|
"title": "List all notes",
|
||||||
|
"description": "List all your available notes",
|
||||||
|
"run_check": true,
|
||||||
|
"request": {},
|
||||||
|
"response": {
|
||||||
|
"notes": [
|
||||||
|
{
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"created": "2021-09-29T13:33:03+01:00",
|
||||||
|
"updated": "2021-09-29T13:33:03+01:00",
|
||||||
|
"title": "New Note",
|
||||||
|
"text": "This is my note"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"update": [{
|
||||||
|
"title": "Update a Note",
|
||||||
|
"description": "Update the note title and text",
|
||||||
|
"run_check": true,
|
||||||
|
"request": {
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"title": "Update Note",
|
||||||
|
"text": "Updated note text"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"created": "2021-09-29T13:33:03+01:00",
|
||||||
|
"updated": "2021-09-29T13:50:20+01:00",
|
||||||
|
"title": "Update Note",
|
||||||
|
"text": "Updated note text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"delete": [{
|
||||||
|
"title": "Delete a Note",
|
||||||
|
"description": "Delete a note by id",
|
||||||
|
"run_check": true,
|
||||||
|
"request": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
|
||||||
|
},
|
||||||
|
"response": {
|
||||||
|
"note": {
|
||||||
|
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
||||||
|
"created": "2021-09-29T13:33:03+01:00",
|
||||||
|
"updated": "2021-09-29T13:50:20+01:00",
|
||||||
|
"title": "Update Note",
|
||||||
|
"text": "Updated note text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
284
notes/handler/notes.go
Normal file
284
notes/handler/notes.go
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/micro/micro/v3/service/client"
|
||||||
|
"github.com/micro/micro/v3/service/errors"
|
||||||
|
"github.com/micro/micro/v3/service/store"
|
||||||
|
pb "github.com/micro/services/notes/proto"
|
||||||
|
"github.com/micro/services/pkg/tenant"
|
||||||
|
streamPb "github.com/micro/services/stream/proto"
|
||||||
|
"google.golang.org/protobuf/types/known/structpb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns an initialized Notes
|
||||||
|
func New(c client.Client) *Notes {
|
||||||
|
return &Notes{
|
||||||
|
Stream: streamPb.NewStreamService("stream", c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notes implements the notes proto definition
|
||||||
|
type Notes struct {
|
||||||
|
Stream streamPb.StreamService
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMessage(ev map[string]interface{}) *structpb.Struct {
|
||||||
|
v, _ := structpb.NewStruct(ev)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create inserts a new note in the store
|
||||||
|
func (h *Notes) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
|
||||||
|
if len(req.Title) == 0 && len(req.Text) == 0 {
|
||||||
|
return errors.BadRequest("notes.create", "missing title and text")
|
||||||
|
}
|
||||||
|
|
||||||
|
tnt, ok := tenant.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
tnt = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate a key (uuid v4)
|
||||||
|
id, err := uuid.NewUUID()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t := time.Now().Format(time.RFC3339)
|
||||||
|
// set the generated fields on the note
|
||||||
|
note := &pb.Note{
|
||||||
|
Id: id.String(),
|
||||||
|
Created: t,
|
||||||
|
Updated: t,
|
||||||
|
Title: req.Title,
|
||||||
|
Text: req.Text,
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%s:%s", tnt, id)
|
||||||
|
rec := store.NewRecord(key, note)
|
||||||
|
|
||||||
|
if err = store.Write(rec); err != nil {
|
||||||
|
return errors.InternalServerError("notes.created", "failed to create note")
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the note in the response
|
||||||
|
rsp.Note = note
|
||||||
|
|
||||||
|
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
|
||||||
|
Topic: "notes",
|
||||||
|
Message: newMessage(map[string]interface{}{
|
||||||
|
"type": "create",
|
||||||
|
"note": note,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Notes) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
|
||||||
|
if len(req.Id) == 0 {
|
||||||
|
return errors.BadRequest("notes.read", "Missing Note ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
tnt, ok := tenant.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
tnt = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%s:%s", tnt, req.Id)
|
||||||
|
|
||||||
|
// read the specific note
|
||||||
|
recs, err := store.Read(key)
|
||||||
|
if err == store.ErrNotFound {
|
||||||
|
return errors.NotFound("notes.read", "Note not found")
|
||||||
|
} else if err != nil {
|
||||||
|
return errors.InternalServerError("notes.read", "Error reading from store: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode the note
|
||||||
|
var note *pb.Note
|
||||||
|
if err := recs[0].Decode(¬e); err != nil {
|
||||||
|
return errors.InternalServerError("notes.update", "Error unmarshaling JSON: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the note
|
||||||
|
rsp.Note = note
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is a unary API which updates a note in the store
|
||||||
|
func (h *Notes) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
||||||
|
// Validate the request
|
||||||
|
if req.Note == nil {
|
||||||
|
return errors.BadRequest("notes.update", "Missing Note")
|
||||||
|
}
|
||||||
|
if len(req.Note.Id) == 0 {
|
||||||
|
return errors.BadRequest("notes.update", "Missing Note ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
tnt, ok := tenant.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
tnt = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%s:%s", tnt, req.Note.Id)
|
||||||
|
|
||||||
|
// read the specific note
|
||||||
|
recs, err := store.Read(key)
|
||||||
|
if err == store.ErrNotFound {
|
||||||
|
return errors.NotFound("notes.update", "Note not found")
|
||||||
|
} else if err != nil {
|
||||||
|
return errors.InternalServerError("notes.update", "Error reading from store: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode the note
|
||||||
|
var note *pb.Note
|
||||||
|
if err := recs[0].Decode(¬e); err != nil {
|
||||||
|
return errors.InternalServerError("notes.update", "Error unmarshaling JSON: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the notes title and text
|
||||||
|
note.Title = req.Note.Title
|
||||||
|
note.Text = req.Note.Text
|
||||||
|
note.Updated = time.Now().Format(time.RFC3339)
|
||||||
|
|
||||||
|
rec := store.NewRecord(key, note)
|
||||||
|
|
||||||
|
// Write the updated note to the store
|
||||||
|
if err = store.Write(rec); err != nil {
|
||||||
|
return errors.InternalServerError("notes.update", "Error writing to store: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
|
||||||
|
Topic: "notes",
|
||||||
|
Message: newMessage(map[string]interface{}{
|
||||||
|
"type": "update",
|
||||||
|
"note": note,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
rsp.Note = note
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Notes) Subscribe(ctx context.Context, req *pb.SubscribeRequest, stream pb.Notes_SubscribeStream) error {
|
||||||
|
backendStream, err := h.Stream.Subscribe(ctx, &streamPb.SubscribeRequest{
|
||||||
|
Topic: "notes",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("notes.subscribe", "Failed to subscribe to notes")
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive messages from the stream
|
||||||
|
msg, err := backendStream.Recv()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ev := msg.Message.AsMap()
|
||||||
|
note := ev["note"].(*pb.Note)
|
||||||
|
|
||||||
|
// filter if necessary by id
|
||||||
|
if len(req.Id) > 0 && note.Id != req.Id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// send back the event to the client
|
||||||
|
if err := stream.Send(&pb.SubscribeResponse{
|
||||||
|
Event: ev["type"].(string),
|
||||||
|
Note: note,
|
||||||
|
}); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes the note from the store, looking up using ID
|
||||||
|
func (h *Notes) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||||
|
// Validate the request
|
||||||
|
if len(req.Id) == 0 {
|
||||||
|
return errors.BadRequest("notes.delete", "Missing Note ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
tnt, ok := tenant.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
tnt = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%s:%s", tnt, req.Id)
|
||||||
|
|
||||||
|
// read the specific note
|
||||||
|
recs, err := store.Read(key)
|
||||||
|
if err == store.ErrNotFound {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return errors.InternalServerError("notes.delete", "Error reading from store: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode the note
|
||||||
|
var note *pb.Note
|
||||||
|
if err := recs[0].Decode(¬e); err != nil {
|
||||||
|
return errors.InternalServerError("notes.delete", "Error unmarshaling JSON: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// now delete it
|
||||||
|
if err := store.Delete(key); err != nil && err != store.ErrNotFound {
|
||||||
|
return errors.InternalServerError("notes.delete", "Failed to delete note")
|
||||||
|
}
|
||||||
|
|
||||||
|
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
|
||||||
|
Topic: "notes",
|
||||||
|
Message: newMessage(map[string]interface{}{
|
||||||
|
"type": "delete",
|
||||||
|
"note": note,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
rsp.Note = note
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List returns all of the notes in the store
|
||||||
|
func (h *Notes) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
||||||
|
tnt, ok := tenant.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
tnt = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%s:", tnt)
|
||||||
|
|
||||||
|
// Retrieve all of the records in the store
|
||||||
|
recs, err := store.Read(key, store.ReadPrefix())
|
||||||
|
if err != nil {
|
||||||
|
return errors.InternalServerError("notes.list", "Error reading from store: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the response notes slice
|
||||||
|
rsp.Notes = make([]*pb.Note, len(recs))
|
||||||
|
|
||||||
|
// Unmarshal the notes into the response
|
||||||
|
for i, r := range recs {
|
||||||
|
if err := r.Decode(&rsp.Notes[i]); err != nil {
|
||||||
|
return errors.InternalServerError("notes.list", "Error decoding note: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
27
notes/main.go
Normal file
27
notes/main.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/micro/micro/v3/service"
|
||||||
|
log "github.com/micro/micro/v3/service/logger"
|
||||||
|
"github.com/micro/services/notes/handler"
|
||||||
|
pb "github.com/micro/services/notes/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// New Service
|
||||||
|
srv := service.New(
|
||||||
|
service.Name("notes"),
|
||||||
|
service.Version("latest"),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialise service
|
||||||
|
srv.Init()
|
||||||
|
|
||||||
|
// Register Handler
|
||||||
|
pb.RegisterNotesHandler(srv.Server(), handler.New(srv.Client()))
|
||||||
|
|
||||||
|
// Run service
|
||||||
|
if err := srv.Run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
1002
notes/proto/notes.pb.go
Normal file
1002
notes/proto/notes.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
253
notes/proto/notes.pb.micro.go
Normal file
253
notes/proto/notes.pb.micro.go
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
|
// source: proto/notes.proto
|
||||||
|
|
||||||
|
package notes
|
||||||
|
|
||||||
|
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 Notes service
|
||||||
|
|
||||||
|
func NewNotesEndpoints() []*api.Endpoint {
|
||||||
|
return []*api.Endpoint{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client API for Notes service
|
||||||
|
|
||||||
|
type NotesService interface {
|
||||||
|
List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error)
|
||||||
|
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
|
||||||
|
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
|
||||||
|
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
|
||||||
|
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
|
||||||
|
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Notes_SubscribeService, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type notesService struct {
|
||||||
|
c client.Client
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNotesService(name string, c client.Client) NotesService {
|
||||||
|
return ¬esService{
|
||||||
|
c: c,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.List", in)
|
||||||
|
out := new(ListResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.Create", in)
|
||||||
|
out := new(CreateResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.Read", in)
|
||||||
|
out := new(ReadResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.Delete", in)
|
||||||
|
out := new(DeleteResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.Update", in)
|
||||||
|
out := new(UpdateResponse)
|
||||||
|
err := c.c.Call(ctx, req, out, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *notesService) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Notes_SubscribeService, error) {
|
||||||
|
req := c.c.NewRequest(c.name, "Notes.Subscribe", &SubscribeRequest{})
|
||||||
|
stream, err := c.c.Stream(ctx, req, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := stream.Send(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ¬esServiceSubscribe{stream}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Notes_SubscribeService interface {
|
||||||
|
Context() context.Context
|
||||||
|
SendMsg(interface{}) error
|
||||||
|
RecvMsg(interface{}) error
|
||||||
|
Close() error
|
||||||
|
Recv() (*SubscribeResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type notesServiceSubscribe struct {
|
||||||
|
stream client.Stream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesServiceSubscribe) Close() error {
|
||||||
|
return x.stream.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesServiceSubscribe) Context() context.Context {
|
||||||
|
return x.stream.Context()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesServiceSubscribe) SendMsg(m interface{}) error {
|
||||||
|
return x.stream.Send(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesServiceSubscribe) RecvMsg(m interface{}) error {
|
||||||
|
return x.stream.Recv(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesServiceSubscribe) Recv() (*SubscribeResponse, error) {
|
||||||
|
m := new(SubscribeResponse)
|
||||||
|
err := x.stream.Recv(m)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server API for Notes service
|
||||||
|
|
||||||
|
type NotesHandler interface {
|
||||||
|
List(context.Context, *ListRequest, *ListResponse) error
|
||||||
|
Create(context.Context, *CreateRequest, *CreateResponse) error
|
||||||
|
Read(context.Context, *ReadRequest, *ReadResponse) error
|
||||||
|
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
|
||||||
|
Update(context.Context, *UpdateRequest, *UpdateResponse) error
|
||||||
|
Subscribe(context.Context, *SubscribeRequest, Notes_SubscribeStream) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterNotesHandler(s server.Server, hdlr NotesHandler, opts ...server.HandlerOption) error {
|
||||||
|
type notes interface {
|
||||||
|
List(ctx context.Context, in *ListRequest, out *ListResponse) error
|
||||||
|
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
|
||||||
|
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
|
||||||
|
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
|
||||||
|
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
|
||||||
|
Subscribe(ctx context.Context, stream server.Stream) error
|
||||||
|
}
|
||||||
|
type Notes struct {
|
||||||
|
notes
|
||||||
|
}
|
||||||
|
h := ¬esHandler{hdlr}
|
||||||
|
return s.Handle(s.NewHandler(&Notes{h}, opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
type notesHandler struct {
|
||||||
|
NotesHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) List(ctx context.Context, in *ListRequest, out *ListResponse) error {
|
||||||
|
return h.NotesHandler.List(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
|
||||||
|
return h.NotesHandler.Create(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
|
||||||
|
return h.NotesHandler.Read(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
|
||||||
|
return h.NotesHandler.Delete(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
|
||||||
|
return h.NotesHandler.Update(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *notesHandler) Subscribe(ctx context.Context, stream server.Stream) error {
|
||||||
|
m := new(SubscribeRequest)
|
||||||
|
if err := stream.Recv(m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return h.NotesHandler.Subscribe(ctx, m, ¬esSubscribeStream{stream})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Notes_SubscribeStream interface {
|
||||||
|
Context() context.Context
|
||||||
|
SendMsg(interface{}) error
|
||||||
|
RecvMsg(interface{}) error
|
||||||
|
Close() error
|
||||||
|
Send(*SubscribeResponse) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type notesSubscribeStream struct {
|
||||||
|
stream server.Stream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesSubscribeStream) Close() error {
|
||||||
|
return x.stream.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesSubscribeStream) Context() context.Context {
|
||||||
|
return x.stream.Context()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesSubscribeStream) SendMsg(m interface{}) error {
|
||||||
|
return x.stream.Send(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesSubscribeStream) RecvMsg(m interface{}) error {
|
||||||
|
return x.stream.Recv(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *notesSubscribeStream) Send(m *SubscribeResponse) error {
|
||||||
|
return x.stream.Send(m)
|
||||||
|
}
|
||||||
91
notes/proto/notes.proto
Normal file
91
notes/proto/notes.proto
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package notes;
|
||||||
|
|
||||||
|
option go_package = "./proto;notes";
|
||||||
|
|
||||||
|
service Notes {
|
||||||
|
rpc List(ListRequest) returns (ListResponse);
|
||||||
|
rpc Create(CreateRequest) returns (CreateResponse);
|
||||||
|
rpc Read(ReadRequest) returns (ReadResponse);
|
||||||
|
rpc Delete(DeleteRequest) returns (DeleteResponse);
|
||||||
|
rpc Update(UpdateRequest) returns (UpdateResponse);
|
||||||
|
rpc Subscribe(SubscribeRequest) returns (stream SubscribeResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
message Note {
|
||||||
|
// unique id for the note, generated if not specified
|
||||||
|
string id = 1;
|
||||||
|
// time at which the note was created
|
||||||
|
string created = 2;
|
||||||
|
// time at which the note was updated
|
||||||
|
string updated = 3;
|
||||||
|
// title of the note
|
||||||
|
string title = 4;
|
||||||
|
// text within the note
|
||||||
|
string text = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new note
|
||||||
|
message CreateRequest {
|
||||||
|
// note title
|
||||||
|
string title = 1;
|
||||||
|
// note text
|
||||||
|
string text = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CreateResponse {
|
||||||
|
// The created note
|
||||||
|
Note note = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a note
|
||||||
|
message ReadRequest {
|
||||||
|
// the note id
|
||||||
|
string id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ReadResponse {
|
||||||
|
// The note
|
||||||
|
Note note = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update a note
|
||||||
|
message UpdateRequest {
|
||||||
|
Note note = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UpdateResponse {
|
||||||
|
Note note = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a note
|
||||||
|
message DeleteRequest {
|
||||||
|
// specify the id of the note
|
||||||
|
string id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteResponse {
|
||||||
|
Note note = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// List all the notes
|
||||||
|
message ListRequest {}
|
||||||
|
|
||||||
|
message ListResponse {
|
||||||
|
// the list of notes
|
||||||
|
repeated Note notes = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specify the note to events
|
||||||
|
message SubscribeRequest {
|
||||||
|
// optionally specify a note id
|
||||||
|
string id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SubscribeResponse {
|
||||||
|
// the event which occured; created, deleted, updated
|
||||||
|
string event = 1;
|
||||||
|
// the note which the operation occured on
|
||||||
|
Note note = 2;
|
||||||
|
}
|
||||||
5
notes/publicapi.json
Normal file
5
notes/publicapi.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "notes",
|
||||||
|
"icon": "📝",
|
||||||
|
"category": "storage"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user