帮助文章

如何使用 Insomnia

TI 以 OpenAPI 文档的形式提供我们的所有 API,这些文档可以导入 Insomnia 等工具中。可以在 https://insomnia.rest/ 上免费下载 Insomnia。

  • 打开 Insomnia
  • 创建新的工程
  • 点击“Create”>“Request collection”
  • 导入 TI OpenAPI 文件
  • 转至“Mange environments”
    • 确保您的基本环境包含以下变量。

                            {
                            "base_url": "{{ scheme }}://{{ host }}{{ base_path }}"
                            }

  • 更新基本环境后,选择 transact.ti.com 环境。
  • 使用您的 TI store API 密钥更新 oauth2ClientId,并使用您的 TI store API 密钥更新 oauth2ClientSecret。


如何使用代码片段

使用 TI API 门户文档中嵌入的 OpenAPI 规范或使用 Insomnia 工具,可以用多种不同的语言为 TI API 生成可在您的实现中使用的代码片段。

在 TI API 门户中:

  • 转到所需的 API 页面。
  • 展开其中一个方法。
  • 点击“试用”。
  • 点击“执行”。
  • 此时将打开代码片段的选项卡页面。

在 Insomnia 中:
 

  • 将集合导入 Insomnia 后,选择该方法,然后点击右侧的向下箭头。
  • 选择“生成代码”。

 

VB.Net 示例

Framework 4.6.1 Imports System.Net Imports System.IO Imports System.Text Imports Newtonsoft.Json.Linq Public Class RESTAPI Public ClientID As String Public ClientSecret As String Private MyToken As RESTAPI.Token Public Function GetPricing(TiPartNumber As String) As String If IsNothing(MyToken) Then GetToken() Else If MyToken.IsValid = False Then GetToken() End If End If Return GetWithOAuth2($"https://transact.ti.com/v2/store/products/{TiPartNumber}?currency=USD&exclude-evms=true",MyToken.Token) End Function Private Sub GetToken() Dim URL As String = "https://transact.ti.com/v1/oauth/accesstoken" Dim Data As String = $"grant_type=client_credentials&client_id={ClientID }&client_secret={ClientSecret}" Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest) request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data) request.ContentLength = byteArray.Length Dim dataStream As Stream = request.GetRequestStream() dataStream.Write(byteArray, 0, byteArray.Length) dataStream.Close() Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) dataStream = response.GetResponseStream() Dim reader As New StreamReader(dataStream) Dim result As String = reader.ReadToEnd() reader.Close() dataStream.Close() response.Close() Dim parsejson As JObject = JObject.Parse(result) MyToken = New Token With {.Time = Now, .Token = parsejson.SelectToken("access_token").ToString } End Sub Public Function GetWithOAuth2(ByVal url As String, ByVal accessToken As String) As String Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) request.Method = "GET" request.Headers.Add("Authorization","Bearer " + accessToken) Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Dim responseStream As Stream = response.GetResponseStream() Dim reader As New StreamReader(responseStream) Dim result As String = reader.ReadToEnd() reader.Close() response.Close() Return result End Function Private Class Token Public Token As String Public Time As DateTime Public Function IsValid() As Boolean If DateDiff(DateInterval.Minute, Now, Time) >= 60 Then Return False Else Return True End If End Function End Class End Class