GithubHelp home page GithubHelp logo

request's Introduction

request

通过预定任务发送网络请求

使用

package request_test

import (
	"testing"

	"github.com/Drelf2018/gorms"
	"github.com/Drelf2018/request"
)

var db = gorms.SetSQLite("jobs.db").AutoMigrate(&request.Job{})
var records = 0

func TestFind(t *testing.T) {
	r := gorms.MustFind[request.Job]()
	records = len(r)
	t.Log(r)
}

func TestCreate(t *testing.T) {
	if records < 2 {
		err := db.Create(
			request.NewGet(
				"https://www.baidu.com",
			).SetQuery(
				request.M{"uid": "12138"},
			).SetHeader(
				request.M{"auth": "admin"},
			).SetCookie("buvid3=BE308E31; i-wanna-go-back=-1;"),
		).Error
		if err != nil {
			t.Fatal(err)
		}
	}
}

func TestDo(t *testing.T) {
	job := request.NewPost(
		"https://postman-echo.com/post",
	).SetData(
		request.M{"data": "abc"},
	).SetQuery(
		request.M{"uid": "114514"},
	).SetHeader(
		request.M{"auth": "admin"},
	).SetCookie(
		"buvid3=ABCDE;",
	).SetReferer(
		"postman-echo.com",
	)
	resp := job.Do()
	err := resp.Error()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(resp.Data())
}

进阶用法

package request_test

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/Drelf2018/request"
)

type Replie struct {
	Member struct {
		Mid   string `json:"mid"`
		Uname string `json:"uname"`
	} `json:"member"`
	Content struct {
		Message string `json:"message"`
	} `json:"content"`
}

func (r Replie) String() string {
	return fmt.Sprintf("%v(%v): %v", r.Member.Uname, r.Member.Mid, r.Content.Message)
}

type ApiData struct {
	Code int `json:"code"`
	Data struct {
		Replies []Replie `json:"replies"`
	} `json:"data"`
}

func TestTypeSession(t *testing.T) {
	session, err := request.NewTypeSession[ApiData](http.MethodGet, "https://api.bilibili.com/x/v2/reply", request.M{
		"pn":   "1",
		"type": "11",
		"oid":  "307611044",
		"sort": "0",
	}.Query)
	if err != nil {
		t.Fatal(err)
	}
	data, err := session.Do()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(data)
}

request's People

Contributors

drelf2018 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.