clients

package
v0.0.0-...-8a8b3aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2024 License: MIT Imports: 5 Imported by: 0

README

vblog 项目的SDK

服务端 restful v1 需要配套提供客户端 client v1

期望

// v1 --> v1.1 (从而对接1.1版本的服务端)
client.blog.CreateBlog()

自己基于Go http.Client 进行 API调用(POST)

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://localhost:7080/api/vblog/v1/blogs/44"
  method := "PATCH"

  payload := strings.NewReader(`{
    "title": "auditor1"
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Authorization", "bearer QGYYwe5gjhx73slPhnlhXP1Z")
  req.Header.Add("Content-Type", "application/json")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))

需要通过看API文档 来知道 URL地址是什么? 参数是什么? 返回是什么?

封装restful client 来专门作为restful api的调用工具,简化调用流程

err := c.c.Post("/").Body(req).Do(ctx).Into(tk)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

实现Vblog的客户端 (ui http ---> restful api) 提供的是一个 lib(SDK) Client.Blog().CreateBlog(xxxx) 不需要知道url, 知道参数是什么,返回什么

func NewClient

func NewClient() *Client

func (*Client) Blog

func (c *Client) Blog() blog.Service

blog service 的客户端调用接口

func (*Client) Token

func (c *Client) Token() token.Service

blog service 的客户端调用接口

type TokenClient

type TokenClient struct {
	// contains filtered or unexported fields
}

func (*TokenClient) Login

func (c *TokenClient) Login(ctx context.Context, req *token.LoginRequest) (
	*token.Token, error)

登录接口(颁发Token) http client /api/vblog/v1/tokens/ --> /api/vblog/v1 + tokens base url http:127.0.0.1:8070/api/vblog/v1/tokens

func (*TokenClient) Logout

退出接口(销毁Token)

func (*TokenClient) ValiateToken

func (c *TokenClient) ValiateToken(context.Context, *token.ValiateToken) (*token.Token, error)

校验Token 是给内部中间层使用 身份校验层 校验完后返回Token, 通过Token获取 用户信息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL