Advanced - Call Api in Programming
We saw in the previous sections that we could do any programing at Pre-request
, ReponseValidation
and PureScript
. One type of programing task we could do is to call apis directly, which just like you clicked the “Run” button for that Apis.
Call Api by Golang
Select the Apis you want to copy, then click copy
button. we can copy Rest APis to current rest project or any other rest project.
Here is the examples to call api:
package api
import "fmt"
import "callapi"
import api13 "restbird/project1/case1/api13"
import api2 "restbird/project2/case3/api2"
//go script api
import api8 "restbird/project3/case5/api8"
import api9 "restbird/project4/case6/api9"
type CallBack struct {}
func (c CallBack) GoScripts(ctx *callapi.RestBirdCtx) bool {
//call rest project 'project1/case1' 's api13 with my current ctx(env)
if err, result, errstr := callapi.DoHttpRequest("project1/case1", "api13", api13.CallBack{}, ctx); err != nil {
fmt.Println("Error happened while api calling")
return false
} else if result {
fmt.Println("api return success")
} else {
fmt.Println("api return failed")
return false
}
//call rest project 'project2/case3' 's api2 with env "Box"
if err, result, errstr := callapi.DoHttpRequestWithEnv("project2/case3", "api2", api2.CallBack{}, "Box"); err != nil {
fmt.Println("Error happened while api calling")
return false
} else if result {
fmt.Println("api return success")
} else {
fmt.Println("api return failed")
return false
}
//call rest project 'project1/case1' 's api13 with my current ctx(env)
if err, result, errstr := callapi.DoGoScript("project3/case5", "api8", api8.CallBack{}, ctx); err != nil {
fmt.Println("Error happened while api calling")
return false
} else if result {
fmt.Println("api return success")
} else {
fmt.Println("api return failed")
return false
}
//call rest project 'project2/case3' 's api2 with env "Box"
if err, result, errstr := callapi.DoGoScriptWithEnv("project4/case6", "api9", api9.CallBack{}, "Office"); err != nil
fmt.Println("Error happened while api calling")
return false
} else if result {
fmt.Println("api return success")
} else {
fmt.Println("api return failed")
return false
}
return true
}
Here is the summary to call api in golang :
- import ‘callapi’
- import the api we will call
- call restapi api interface is DoHttpRequest and DoHttpRequestWithEnv.
The difference is DoHttpRequestWithEnv will setup an new ctx with the specified env varible. - call pure script api interface is DoGoScript and DoGoScriptWithEnv.
The difference is DoGoScriptWithEnv will setup a new ctx with specified env varible
Call Api By Python
Here is the examples to call api by Python:
# encoding: utf-8
import requests
from restapi import RestApi
def PythonScripts(ctx) :
api = RestApi("Test/testpy", "api0", ctx)
result = api.runApi()
if result == True :
print ("call api success")
else:
print ("call api failed")
return result