B4J Question [BANano] [SOLVED] Ajax Calls: How to Convert B4J Http Calls?

Discussion in 'B4J Questions' started by Mashiane, Dec 5, 2018.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    I have'nt used this yet, i'm curious about your ajax calls. This is my case scenario.

    1. GetAccessToken - I save the retrieved access token to a file. I can save this to localstorage in this case using BANano I think.
    2. Used the saved access token to return other data from the rest api...

    Here is how I am currently using it.

    Step 1:

    Code:
    'get the access token
    Sub GetAccessToken() As ResumableSub
        
    Log("GetAccessToken")
        
    'set access token to blank
        access_token = ""
        
    Dim job As HttpJob
        
    'initialize a http request
        job.Initialize("",Me)
        
    'pass the url to get the token
        job.PostString("<endpoint>",$"grant_type=password&username=${userName}&password=${passWord}"$)
        
    'set the content type
        job.GetRequest.SetContentType("application/x-www-form-urlencoded")
        
    'wait for the web service to complete
        Wait For (job) JobDone(job As HttpJob)
        
    If job.Success Then
            
    'translate to a map
            Dim strResponse As String = job.GetString
            job.release
            
    Dim access As Map = Json2Map(strResponse)
            
    'assign variables
            access_token = access.GetDefault("access_token","")
            expires_in = access.GetDefault(
    "expires_in","")
            
    'save the token to a file
            File.WriteString(File.DirApp,"access_token.txt",access_token)
            
    Return True
        
    Else
            
    If File.Exists(File.DirApp,accessTokenFile) Then File.Delete(File.DirApp,accessTokenFile)
            LogError(
    $"A '${job.ErrorMessage}' has been experienced reading GetAccessToken!"$)
            job.Release
            
    Return False
        
    End If
    End Sub
    Step 2: Subsequent

    2. Next calls to the rest api, i pass an endpoint and a header bearer with the user token. I have a couple of API calls that work like this.

    Code:
    Sub ManyCallsLikeThis(ID As Int) As ResumableSub
        
    Log("ManyCallsLikeThis")
        
    'read the access token from file
        ReadAccessToken
        
    If access_token = "" Then Return False
        
    'define command to read the province delimitation
        Dim strCommand As String = $"<EndPoint>=${ID}"$
        
    'create an http request
        Dim job As HttpJob
        job.Initialize(
    "",Me)
        job.Download(strCommand)
        
    'pass the access token
        job.GetRequest.SetHeader("Authorization""bearer " & access_token)
        
    'wait until the job is finished
        Wait For (job) JobDone(job As HttpJob)
        
    If job.Success Then
            
    'get the response
            Dim strResponse As String = job.GetString
            job.release
            
    Dim Result As Map = Json2Map(strResponse)
            
    'get the party results
            Dim pr As List = Result.Get("pr")
            
    For Each prmap As Map In pr
            
    'do some things
    Next
            
    Return True
        
    Else
            LogError(
    $"A '${job.ErrorMessage}' has been experienced reading the ManyCallsLikeThis!"$)
            job.release
            
    Return False
        
    End If
    End Sub
    Question:

    I just need advise on how I can perform this inside BANano AjaxCalls?

    Thanks a lot.
     
    joulongleu likes this.
  2. alwaysbusy

    alwaysbusy Expert Licensed User

    I think this would need some change to the CallAjax methods to allow setting headers, but in theory it could look something like this.

    Code:
    Dim access_token As String = ""
    Dim userName As String = ""
    Dim Password As String = ""
    Dim expires_in As Int

    public Sub GetAccessToken() 
       
    Dim headers As Map
       headers.Initialize
       headers.put(
    "Content-Type""application/x-www-form-urlencoded")
       
    Dim strResponse As String = BANano.CallAjaxWait("http://yoururl.com""POST""json"$"grant_type=password&username=${userName}&password=${Password}"$,False, headers)
       
    Dim json As BANanoJSONParser
       
    Try
           json.Initialize(strResponse)
           
    Dim access As Map = json.NextObject
           
    'assign variables
           access_token = access.GetDefault("access_token","")
           expires_in = access.GetDefault(
    "expires_in","")
      
           BANano.SetCookie(
    "mytoken", access_token, $"{"expires": ${expires_in}}"$)
       
    Catch
           BANano.RemoveCookie(
    "mytoken""")
       
    End Try
    End Sub

    public Sub ManyCallsLikeThis(ID As Int)
       
    Dim access_token As String = ""
       access_token = BANano.GetCookie(
    "mytoken")
       
    If access_token = "" Then Return
      
       
    Dim headers As Map
       headers.Initialize
       headers.put(
    "Authorization""bearer " & access_token)
       
    Dim strResponse As String = BANano.CallAjaxWait("http://yoururl.com""GET""json"$"<EndPoint>=${ID}"$,False, headers)
       
    Dim json As BANanoJSONParser
       
    Try
           json.Initialize(strResponse)
           
    Dim Result As Map = json.NextObject
           
    Dim pr As List = Result.Get("pr")
           
    For Each prmap As Map In pr
               
    'do some things
           Next
       
    Catch
           
    Log(LastException.Message)      
       
    End Try
    End Sub
    But I would need access to your real REST API to test this theory.
     
    Last edited: Dec 6, 2018
    joulongleu and Mashiane like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice