Foxtable(狐表)用户栏目专家坐堂 → 调整C#转换过来的代码


  共有1287人关注过本帖树形打印复制链接

主题:调整C#转换过来的代码

帅哥哟,离线,有人找我吗?
lxhmax
  1楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:636 积分:5699 威望:0 精华:0 注册:2012/8/2 19:04:00
调整C#转换过来的代码  发帖心情 Post By:2024/3/27 15:57:00 [只看该作者]

老师,下面这段代码是在https://converter.telerik.com/转换过来的代码,放到全局代码保存保存,请问下要怎么调整下?

Imports System
Imports System.Net
Imports System.Collections.Generic
Imports RestSharp
Imports Newtonsoft.Json.Linq

Namespace wayfair_integration
    Class GraphiQL_Code_Template
        Shared CLIENT_ID As String = "{YOUR_CLIENT_ID}"
        Shared CLIENT_SECRET As String = "{YOUR_CLIENT_SECRET}"
        Shared API_URL As String = "https://api.wayfair.com/v1/graphql"
        Shared AUTH_URL As String = "https://sso.auth.wayfair.com/oauth/token"
        Shared QUERY As String = "query identity {identity  {id,userType,username,firstName,lastName,email}}"
        Shared VARIABLES As String = "null"

        Private Shared Function SendRequest(ByVal method As Method, ByVal url As String, ByVal Optional bodyContentType As String = "", ByVal Optional body As String = "", ByVal Optional headers As Dictionary(Of String, String) = Nothing) As IRestResponse
            Dim client As RestClient = New RestClient(url)
            Dim request As RestRequest = New RestRequest(method)

            If headers IsNot Nothing Then

                For Each header As KeyValuePair(Of String, String) In headers
                    request.AddHeader(header.Key, header.Value)
                Next
            End If

            If method = Method.POST Then
                request.AddParameter(bodyContentType, body, ParameterType.RequestBody)
            End If

            Dim response As IRestResponse = client.Execute(request)
            Dim responseStatus As HttpStatusCode = response.StatusCode

            Select Case responseStatus
                Case HttpStatusCode.OK
                    Return response
                Case Else
                    Throw New Exception($"Request failed with status {response.StatusDescription}, response {response.Content}")
            End Select
        End Function

        Private Shared Function SendGraphQLRequest(ByVal token As String, ByVal query As String, ByVal variables As String) As JObject
            Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)()
            headers.Add("content-type", "application/json")
            headers.Add("cache-control", "no-cache")
            headers.Add("authorization", $"Bearer {token}")
            Dim graphQLOperation As String = $" ""query"": ""{query}"", ""variables"": {variables}"
            Dim graphQLPayload As String = $"{{{graphQLOperation}}}"

            Try
                Dim response As IRestResponse = SendRequest(Method.POST, API_URL, "application/json", graphQLPayload, headers)
                Return JObject.Parse(response.Content)
            Catch e As Exception
                Console.WriteLine($"Problem executing the GraphQL request: {e.Message}")
                System.Environment.[Exit](1)
            End Try

            Return Nothing
        End Function

        Private Shared Function FetchToken(ByVal clientID As String, ByVal clientSecret As String) As String
            Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)()
            headers.Add("content-type", "application/json")
            headers.Add("cache-control", "no-cache")
            Dim authCredentials As String = $"
                ""grant_type"": ""client_credentials"",
                ""client_id"": ""{clientID}"",
                ""client_secret"": ""{clientSecret}"",
                ""audience"": ""https://api.wayfair.com/""
            "
            Dim authPayload As String = $"{{{authCredentials}}}"

            Try
                Dim response As IRestResponse = SendRequest(Method.POST, AUTH_URL, "application/json", authPayload, headers)
                Return CStr(JObject.Parse(response.Content)("access_token"))
            Catch e As Exception
                Console.WriteLine($"Could not retrieve a token for the request: {e.Message}")
                System.Environment.[Exit](1)
            End Try

            Return ""
        End Function

        Private Shared Sub Main(ByVal args As String())
            Dim myToken As String = FetchToken(CLIENT_ID, CLIENT_SECRET)
            Dim graphQLResponse As JObject = SendGraphQLRequest(myToken, QUERY, VARIABLES)
            Console.WriteLine(graphQLResponse)
        End Sub
    End Class
End Namespace


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110544 积分:562604 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/3/27 16:15:00 [只看该作者]

RestSharp这个第三方组件复制到Foxtable的安装目录,注意这个组件要支持net framework 4.0
1、去掉【Namespace wayfair_integration】这一行及前面所有代码,去掉最后一行
2、Class GraphiQL_Code_Template改为public Class GraphiQL_Code_Template
3、Private 全部改为public
4、把命名空间添加到代码里,比如

        public Shared Function SendRequest(ByVal method As RestSharp.Method, ByVal url As String, ByVal Optional bodyContentType As String = "", ByVal Optional body As String = "", ByVal Optional headers As Dictionary(Of String, String) = Nothing) As RestSharp.IRestResponse
            Dim client As New RestSharp.RestClient(url)


 回到顶部
帅哥哟,离线,有人找我吗?
lxhmax
  3楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:636 积分:5699 威望:0 精华:0 注册:2012/8/2 19:04:00
  发帖心情 Post By:2024/3/27 16:38:00 [只看该作者]

老师,下面是我取的部分代码,这么改对吗?

Public Class GraphiQL_Code_Template

Public Shared Function SendGraphQLRequest(ByVal token As String, ByVal query As String, ByVal variables As String) As JObject
Dim headers As Dictionary(Of String, String) = New Dictionary(Of String, String)()
headers.Add("content-type", "application/json")
headers.Add("cache-control", "no-cache")
headers.Add("authorization", "Bearer qvjtdxwOZVJTzeBF0xxUtJlpn52UwuiJyoasKyMfKzx7L0iTXwLAc")

'Dim graphQLOperation As String = $" ""query"": ""{query}"", ""variables"": {variables}"
'Dim graphQLPayload As String = $"{{{graphQLOperation}}}"
上面的蓝色是源代码,我改成下面这样对吗?
Dim graphQLOperation As String = "query: {id,userType,username,firstName,lastName,email},variables: {null}"
Dim graphQLPayload As String = "query: {id,userType,username,firstName,lastName,email},variables: {null}"

Try
    Dim response As IRestResponse = SendRequest(Method.POST, "https://api.wayfair.com/v1/graphql", "application/json", graphQLPayload, headers)
    Return JObject.Parse(response.Content)
Catch e As Exception
    Console.WriteLine($"Problem executing the GraphQL request: {e.Message}")
    System.Windows.Forms.TextBox.Environment.[Exit](1)
End Try

Return Nothing
End Function

End Class

保存的时候提示
编译错误:未定义类型"IRestResponse”
锴误代码:Dim response As lRestResponse =SendRequest(Method.PosT,"https://api.wayfair.com/v1/graphql"application/json",graphQLPayload, headers)


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110544 积分:562604 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/3/27 16:55:00 [只看该作者]

红色这个我也不清楚,去看组件的文档吧

提示错误的这个加上命名空间,参考2楼第4点

 回到顶部
帅哥哟,离线,有人找我吗?
lxhmax
  5楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:636 积分:5699 威望:0 精华:0 注册:2012/8/2 19:04:00
  发帖心情 Post By:2024/3/27 17:40:00 [只看该作者]

下面转换的VB代码
Private Sub SurroundingSub() Dim client = New RestClient("https://api.wayfair.com/v1/graphql") client.Timeout = -1 Dim request = New RestRequest(Method.POST) request.AddHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsImt") request.AddHeader("Cache-Control", "no-cache") client.UserAgent = "Apifox/1.0.0 (https://apifox.com)" request.AddHeader("Content-Type", "application/json") request.AddHeader("Accept", "*/*") request.AddHeader("Host", "api.wayfair.com") request.AddHeader("Connection", "keep-alive") request.AddParameter("application/json", "{""query"":""query {\r\n identity {\r\n username,\r\n email,\r\n applications {\r\n name\r\n }\r\n }\r\n}"",""variables"":{}}", ParameterType.RequestBody) Dim response As IRestResponse = client.Execute(request) Console.WriteLine(response.Content) End Sub 报错如下,要怎么处理? 编译错误:未定义类型~RestClient” 错退代码:Dim client = NewRestclient("https://api.wayfair.com/v1/graphql")
[此贴子已经被作者于2024/3/27 17:43:00编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110544 积分:562604 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/3/28 8:28:00 [只看该作者]

加上命名空间

 回到顶部