예제 #1
0
func main() {

	// Start Session
	s := napping.Session{}
	url := BASE_URL + "get/sample"
	fmt.Println("URL:>", url)

	fmt.Println("--------------------------------------------------------------------------------")
	println("")

	fooParams := napping.Params{"md5": SAMPLE, "apikey": API_KEY}
	p := fooParams

	res := ResponseUserAgent{}
	resp, err := s.Post(url, &p, &res, nil)
	if err != nil {
		fmt.Println("Error in request")
	}
	//
	// Process response
	//
	println("")
	fmt.Println("response Status:", resp.Status())
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("Header")
	fmt.Println(resp.HttpResponse().Header)
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("RawText")
	fmt.Println(resp.RawText())
	println("")
}
func instruct(a *Coaction) *NodeResp {
	fmt.Println("Instructed " + a.Instruction + " for id " + strconv.Itoa(a.Id))
	v, _ := streamlut[a.Id]
	url := "http://127.0.0.1:" + strconv.Itoa(v.Rconf.Lport) + "/player/" + a.Instruction
	fmt.Println("Instructed as " + url)
	var answer NodeResp
	e := struct {
		Message string
		Errors  []struct {
			Resource string
			Field    string
			Code     string
		}
	}{}
	s := napping.Session{}

	var forwarder *Forwarder = nil

	if (a.Instruction == "alterviewpoint") || (a.Instruction == "mouseclick") {
		forwarder = &Forwarder{a.Cmdargs}
	}
	resp, err := s.Post(url, forwarder, &answer, &e)

	if err != nil {
		return &NodeResp{false, err.Error()}
	}

	if resp.Status() != 200 {
		return &NodeResp{false, strconv.Itoa(resp.Status()) + "..." + e.Message}
	}

	return &answer
}
예제 #3
0
파일: httpbin.go 프로젝트: Skarlso/slick
func main() {
	//
	// Struct to hold error response
	//
	e := struct {
		Message string
	}{}

	// Start Session
	s := napping.Session{}
	url := "http://httpbin.org/user-agent"
	fmt.Println("URL:>", url)

	res := ResponseUserAgent{}
	resp, err := s.Get(url, nil, &res, nil)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	fmt.Println("response Status:", resp.Status())

	if resp.Status() == 200 {
		// fmt.Printf("Result: %s\n\n", resp.response)
		// resp.Unmarshal(&e)
		fmt.Println("res:", res.Useragent)
	} else {
		fmt.Println("Bad response status from Github server")
		fmt.Printf("\t Status:  %v\n", resp.Status())
		fmt.Printf("\t Message: %v\n", e.Message)
	}
	fmt.Println("--------------------------------------------------------------------------------")
	println("")

	url = "http://httpbin.org/get"
	fmt.Println("URL:>", url)
	fooParams := napping.Params{"foo": "bar"}
	p := fooParams

	res = ResponseUserAgent{}
	resp, err = s.Get(url, &p, &res, nil)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	fmt.Println("response Status:", resp.Status())
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("Header")
	fmt.Println(resp.HttpResponse().Header)
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("RawText")
	fmt.Println(resp.RawText())
	println("")
}
예제 #4
0
func (u *UserInfo) Get(token oauth2.Token) {
	ns := napping.Session{}
	ns.Header = &http.Header{}
	ns.Header.Add("Authorization", "Bearer "+token.AccessToken)
	url := settings.UnifiedApiEndpointUrl + "/me"
	resp, err := ns.Get(url, nil, &u, nil)
	if err != nil {
		log.Fatal(err)
	}
	if resp.Status() == 200 {
		fmt.Println("Loaded profile for :", u.Displayname)
	} else {
		fmt.Println("Bad response status from office365 unified api")
		fmt.Println("Raw Response Text : %s", resp.RawText())
	}
}
예제 #5
0
func Get(url string, token oauth2.Token) string {
	ns := napping.Session{}
	ns.Header = &http.Header{}
	ns.Header.Add("Authorization", "Bearer "+token.AccessToken)
	resp, err := ns.Get(url, nil, nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Status : %i", resp.Status())
	fmt.Println("Server response %s", resp.RawText())

	dst := new(bytes.Buffer)
	src := []byte(resp.RawText())

	json.Indent(dst, src, "", "\t")

	return dst.String()
}
예제 #6
0
func callPeer(watchmetadata *Watchmedata, destination *utilities.OPData, queue *lane.Queue, verb string, wg *sync.WaitGroup) {
	defer wg.Done()

	target := "http://" + destination.Ovip + ":" + strconv.Itoa(destination.Announceport) + "/ovp"
	url := target + "/" + verb
	fmt.Println("Instructed as " + url)
	e := struct {
		Message string
		Errors  []struct {
			Resource string
			Field    string
			Code     string
		}
	}{}

	var j map[string]interface{}

	s := napping.Session{}

	resp, err := s.Post(url, watchmetadata, &j, &e)

	if err != nil {
		log.Printf("failed to " + verb + " on " + target + " because " + err.Error())
		return
	}

	if resp.Status() != 200 {
		log.Printf("failed to " + verb + " on " + target + " because " + strconv.Itoa(resp.Status()) + "..." + e.Message)
		return
	}

	if verb == "withdraw" || verb == "update" {
		source := watchmetadata.OPConfig
		if j["Status"].(bool) {
			queue.Enqueue(destination)
		} else {
			mesg := fmt.Sprintf("Not "+verb+" %+v from %+v", source, destination)
			panic(mesg)
		}
	}
	queue.Enqueue(*destination)
}
예제 #7
0
func getRESTResponse(url string, p *url.Values, result interface{}) {
	//
	// Struct to hold error response
	//
	e := struct {
		Message string
	}{}

	s := napping.Session{}
	resp, err := s.Get(url, p, result, e)
	if err != nil {
		log.Fatal(err)
	}
	Trace.Printf("%s Return Status [%v]", url, resp.Status())
	if resp.Status() == 200 {
		return
	} else {
		log.Fatal("Resource could not be fetched")
		log.Fatal("\t Status:  %v\n", resp.Status())
		log.Fatal("\t Message: %v\n", e.Message)
	}
}
예제 #8
0
func (s *TestSuite) TestAllRight(c *C) {
	BASE_URL := "http://127.0.0.1:9999"
	cur_dir, err := os.Getwd()
	if err != nil {
		c.Error(err)
	}
	test_file := path.Join(cur_dir, "temp/a.png")
	finite_url := BASE_URL + test_file
	log.Println(finite_url)

	// ============ authenticate ==================
	session := napping.Session{
		Userinfo: url.UserPassword("admin", "admin"),
	}
	//  ==========================================
	for i := 1; i <= 10; i++ {
		log.Println("Working ", i)
		result := IdioticJSON{}
		payload := StandartJSON{}
		payload["Artist"] = uuid.NewV4().String()
		payload["Author"] = uuid.NewV4().String()
		payload["Comment"] = uuid.NewV4().String()
		payload["Copyright"] = uuid.NewV4().String()
		// wait for server to be up ..
		_, err = session.Post(finite_url, &payload, &result, nil)
		for err != nil {
			_, err := session.Post(finite_url, &payload, &result, nil)
			if err == nil {
				break
			}
		}
		c.Assert(result.Items[0]["Artist"], Equals, payload["Artist"])
		c.Assert(result.Items[0]["Author"], Equals, payload["Author"])
		c.Assert(result.Items[0]["Comment"], Equals, payload["Comment"])
		c.Assert(result.Items[0]["Copyright"], Equals, payload["Copyright"])
	}
}
예제 #9
0
func goCrestEMDRBridge() {

	var err error

	type regionKey struct {
		RegionID int64
		TypeID   int64
	}

	type marketRegions struct {
		RegionID   int64  `db:"regionID"`
		RegionName string `db:"regionName"`
	}

	type marketTypes struct {
		TypeID   int64  `db:"typeID"`
		TypeName string `db:"typeName"`
	}

	// Pool of CREST sessions
	crestSession := napping.Session{}
	regions := []marketRegions{}
	types := []marketTypes{}
	stations = make(map[int64]int64)

	// Scope to allow garbage colect to reclaim startup data.
	{
		type crestRegions_s struct {
			TotalCount_Str string
			Items          []struct {
				HRef string
				Name string
			}
			PageCount  int64
			TotalCount int64
		}

		type crestTypes_s struct {
			TotalCount_Str string
			Items          []struct {
				Type struct {
					ID   int64
					Name string
				}
			}
			PageCount  int64
			TotalCount int64
			Next       struct {
				HRef string `json:"href,omitempty"`
			}
		}

		// Collect Regions from CREST servers.
		crestRegions := crestRegions_s{}
		_, err = crestSession.Get(crestUrl+"regions/", nil, &crestRegions, nil)
		fatalCheck(err)

		// Extract the ID out of the URI.
		for _, r := range crestRegions.Items {
			re := regexp.MustCompile("([0-9]+)")
			regionID, _ := strconv.ParseInt(re.FindString(r.HRef), 10, 64)
			regions = append(regions, marketRegions{regionID, r.Name})
		}
		log.Printf("Loaded %d Regions", len(regions))

		// Collect Types from CREST servers.
		crestTypes := crestTypes_s{}
		_, err = crestSession.Get(crestUrl+"market/types/", nil, &crestTypes, nil)
		fatalCheck(err)

		// Translate the first page.
		for _, t := range crestTypes.Items {
			types = append(types, marketTypes{t.Type.ID, t.Type.Name})
		}

		// Loop the next pages.
		for {
			last := crestTypes.Next.HRef

			_, err = crestSession.Get(crestTypes.Next.HRef, nil, &crestTypes, nil)
			fatalCheck(err)
			for _, t := range crestTypes.Items {
				types = append(types, marketTypes{t.Type.ID, t.Type.Name})
			}

			if crestTypes.Next.HRef == last {
				break
			}
		}

		log.Printf("Loaded %d Types", len(types))

		// Load NPC stations from file.
		file, err := os.Open("stations")
		fatalCheck(err)
		defer file.Close()
		reader := csv.NewReader(file)
		reader.Comma = '\t' // Tab delimited.

		for {
			record, err := reader.Read()
			if err == io.EOF {
				break
			}
			stationID, err := strconv.ParseInt(record[0], 10, 64)
			fatalCheck(err)
			systemID, err := strconv.ParseInt(record[1], 10, 64)
			fatalCheck(err)
			stations[stationID] = systemID
		}
		log.Printf("Loaded %d NPC Stations", len(stations))

		// Load player stations from API
		getStationsFromAPI()
		log.Printf("Added Player Stations: %d Total Stations", len(stations))
	}

	// FanOut response channel for posters
	postChannel := make(chan []byte)

	// Pool of transports.
	transport := &http.Transport{DisableKeepAlives: false}
	client := &http.Client{Transport: transport}

	go func() {
		for i := 0; i < 11; i++ {
			// Don't spawn them all at once.
			time.Sleep(time.Second / 2)

			go func() {
				for {
					msg := <-postChannel

					response, err := client.Post(uploadUrl, "application/json", bytes.NewBuffer(msg))
					if err != nil {
						log.Println("EMDRCrestBridge:", err)
					} else {
						if response.Status != "200 OK" {
							body, _ := ioutil.ReadAll(response.Body)
							log.Println("EMDRCrestBridge:", string(body))
							log.Println("EMDRCrestBridge:", string(response.Status))
						}
						// Must read everything to close the body and reuse connection
						ioutil.ReadAll(response.Body)
						response.Body.Close()
					}
				}
			}()
		}
	}()
	// Throttle Crest Requests
	rate := time.Second / 30
	throttle := time.Tick(rate)

	// semaphore to prevent runaways
	sem := make(chan bool, maxGoRoutines)
	sem2 := make(chan bool, maxGoRoutines)

	for {
		// loop through all regions
		for _, r := range regions {
			log.Printf("Scanning Region: %s", r.RegionName)
			// and each item per region
			for _, t := range types {
				<-throttle // impliment throttle
				sem2 <- true

				rk := regionKey{r.RegionID, t.TypeID}
				go func() {
					defer func() { <-sem2 }()
					// Process Market History
					h := marketHistory{}
					url := fmt.Sprintf("https://public-crest.eveonline.com/market/%d/types/%d/history/", rk.RegionID, rk.TypeID)

					response, err := crestSession.Get(url, nil, &h, nil)
					if err != nil {
						log.Printf("EMDRCrestBridge: %s", err)
						return
					}
					if response.Status() == 200 {
						sem <- true
						go postHistory(sem, postChannel, h, rk.RegionID, rk.TypeID)
					}
				}()

				sem2 <- true
				go func() {
					defer func() { <-sem2 }()
					// Process Market Buy Orders
					b := marketOrders{}
					url := fmt.Sprintf("https://public-crest.eveonline.com/market/%d/orders/buy/?type=https://public-crest.eveonline.com/types/%d/", rk.RegionID, rk.TypeID)

					response, err := crestSession.Get(url, nil, &b, nil)
					if err != nil {
						log.Printf("EMDRCrestBridge: %s", err)
						return
					}
					if response.Status() == 200 {
						sem <- true
						go postOrders(sem, postChannel, b, 1, rk.RegionID, rk.TypeID)
					}
				}()

				sem2 <- true
				go func() {
					defer func() { <-sem2 }()
					// Process Market Sell Orders
					s := marketOrders{}
					url := fmt.Sprintf("https://public-crest.eveonline.com/market/%d/orders/sell/?type=https://public-crest.eveonline.com/types/%d/", rk.RegionID, rk.TypeID)

					response, err := crestSession.Get(url, nil, &s, nil)
					if err != nil {
						log.Printf("EMDRCrestBridge: %s", err)
						return
					}
					if response.Status() == 200 {
						sem <- true
						go postOrders(sem, postChannel, s, 0, rk.RegionID, rk.TypeID)
					}
				}()
			}
		}
	}
}
예제 #10
0
func main() {
	//
	// Prompt user for Github username/password
	//
	var username string
	fmt.Printf("Github username: "******"%s", &username)
	if err != nil {
		log.Fatal(err)
	}
	passwd, err := gopass.GetPass("Github password: "******"scopes"`
		Note   string   `json:"note"`
	}{
		Scopes: []string{"public_repo"},
		Note:   "testing Go napping" + time.Now().String(),
	}
	//
	// Struct to hold response data
	//
	res := struct {
		Id        int
		Url       string
		Scopes    []string
		Token     string
		App       map[string]string
		Note      string
		NoteUrl   string `json:"note_url"`
		UpdatedAt string `json:"updated_at"`
		CreatedAt string `json:"created_at"`
	}{}
	//
	// Struct to hold error response
	//
	e := struct {
		Message string
		Errors  []struct {
			Resource string
			Field    string
			Code     string
		}
	}{}
	//
	// Setup HTTP Basic auth for this session (ONLY use this with SSL).  Auth
	// can also be configured on a per-request basis when using Send().
	//
	s := napping.Session{
		Userinfo: url.UserPassword(username, passwd),
	}
	url := "https://api.github.com/authorizations"
	//
	// Send request to server
	//
	resp, err := s.Post(url, &payload, &res, &e)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	if resp.Status() == 201 {
		fmt.Printf("Github auth token: %s\n\n", res.Token)
	} else {
		fmt.Println("Bad response status from Github server")
		fmt.Printf("\t Status:  %v\n", resp.Status())
		fmt.Printf("\t Message: %v\n", e.Message)
		fmt.Printf("\t Errors: %v\n", e.Message)
		pretty.Println(e.Errors)
	}
	println("")
}
예제 #11
0
파일: api.go 프로젝트: mrcsparker/ifin
func Setup() napping.Session {
	s := napping.Session{Log: false}
	s.Client = &http.Client{}
	return s
}
예제 #12
0
파일: util.go 프로젝트: skendall/f5er
func SendRequest(u string, method int, sess *napping.Session, pload interface{}, res interface{}) (error, *napping.Response) {

	//
	// Send request to server
	//
	e := httperr{}
	var (
		err  error
		resp *napping.Response
	)
	sess.Log = debug

	switch method {
	case GET:
		resp, err = sess.Get(u, nil, &res, &e)
	case POST:
		resp, err = sess.Post(u, &pload, &res, &e)
	case PUT:
		resp, err = sess.Put(u, &pload, &res, &e)
	case PATCH:
		resp, err = sess.Patch(u, &pload, &res, &e)
	case DELETE:
		resp, err = sess.Delete(u, &res, &e)
	}

	if err != nil {
		return err, resp
	}
	if resp.Status() == 401 {
		return errors.New("unauthorised - check your username and passwd"), resp
	}
	if resp.Status() >= 300 {
		return errors.New(e.Message), resp
	} else {
		// all is good in the world
		return nil, resp
	}
}