little-cui commented on a change in pull request #532: Issues-531 add 
registration and discovery for client
URL: 
https://github.com/apache/servicecomb-service-center/pull/532#discussion_r256676685
 
 

 ##########
 File path: pkg/client/sc/microservice.go
 ##########
 @@ -0,0 +1,117 @@
+package sc
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "net/url"
+
+       "github.com/apache/servicecomb-service-center/server/core"
+       pb "github.com/apache/servicecomb-service-center/server/core/proto"
+       scerr "github.com/apache/servicecomb-service-center/server/error"
+)
+
+const (
+       apiExistenceURL     = "/v4/%s/registry/existence"
+       apiMicroServicesURL = "/v4/%s/registry/microservices"
+       apiMicroServiceURL  = "/v4/%s/registry/microservices/%s"
+)
+
+func (c *SCClient) CreateService(ctx context.Context, domainProject string, 
service *pb.MicroService) (string, *scerr.Error) {
+       domain, project := core.FromDomainProject(domainProject)
+       headers := c.CommonHeaders(ctx)
+       headers.Set("X-Domain-Name", domain)
+
+       reqBody, err := json.Marshal(&pb.CreateServiceRequest{Service: service})
+       if err != nil {
+               return "", scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+
+       resp, err := c.RestDoWithContext(ctx, http.MethodPost,
+               fmt.Sprintf(apiMicroServicesURL, project),
+               headers, reqBody)
+       if err != nil {
+               return "", scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+       defer resp.Body.Close()
+
+       body, err := ioutil.ReadAll(resp.Body)
+       if err != nil {
+               return "", scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+
+       if resp.StatusCode != http.StatusOK {
+               return "", c.toError(body)
+       }
+
+       serviceResp := &pb.CreateServiceResponse{}
+       err = json.Unmarshal(body, serviceResp)
+       if err != nil {
+               return "", scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+       return serviceResp.ServiceId, nil
+}
+
+func (c *SCClient) DeleteService(ctx context.Context, domainProject, serviceId 
string) *scerr.Error {
+       domain, project := core.FromDomainProject(domainProject)
+       headers := c.CommonHeaders(ctx)
+       headers.Set("X-Domain-Name", domain)
+
+       resp, err := c.RestDoWithContext(ctx, http.MethodDelete,
+               fmt.Sprintf(apiMicroServiceURL, project, serviceId),
+               headers, nil)
+       if err != nil {
+               return scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+       defer resp.Body.Close()
+
+       body, err := ioutil.ReadAll(resp.Body)
+       if err != nil {
+               return scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+
+       if resp.StatusCode != http.StatusOK {
+               return c.toError(body)
+       }
+
+       return nil
+}
+
+func (c *SCClient) ServiceExistence(ctx context.Context, domainProject string, 
service *pb.MicroService) (string, *scerr.Error) {
 
 Review comment:
   the input parameters should be env, appId, serviceName, versionRule, not 
pb.MicroService

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to