Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

help with net.HTTPClient()


Question

Posted (edited)

Any experts on the net.HTTPClient() coding in here?

 

Am trying to build a QA for my car charger (Charge Amps HALO) and have access to the REST API. They have a bit different implementation to what I have worked with before, you need to login with email, password & APIkey to get a ”token” that you then use in other request. If I do this login on their swagger page I am able to get a token and then build a simple function to pull out information about my Charger, and this works just fine:

 

Code:

function QuickApp:GetData()
  http = net.HTTPClient()
    local url = "

Please login or register to see this link.

d"
    local requestBody = '

Please login or register to see this link.

d'
    local token 
="aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"
 
    http:request(url, 
          {
            options = {
                data = requestBody, 
                method = "GET",
                headers = {
                           ["accept"] = "application/json"
                           ["Authorization"] = "Bearer " ..token
                           }
                       },
         success = function(response) 
                      print("response status: " ..response.status)
                      print("Response data: " ..response.data)
                    end
            })
end

Request URL from the Charge Amps swagger page>

Please login or register to see this code.

Curl from the Charge Amps swagger page>

Please login or register to see this code.

 

200

Response body from the Charge Amps swagger page>

Please login or register to see this code.

 

Log output in Fibaro HC3>

[21.04.2022] [18:34:25] [DEBUG] [QUICKAPP977]: response status: 200
[21.04.2022] [18:34:25] [DEBUG] [QUICKAPP977]: Response data: [{"id":"XXXXXXXXXXXXXXX","name":"XXXXXXXXXXXX","password":"XXXXXXXXXX","type":"HALO","isLoadbalanced":true,"firmwareVersion":"72","hardwareVersion":"2","ocppVersion":null,"settings":null,"connectors":[{"chargePointId":"XXXXXXXXXX","connectorId":1,"type":"Charger","settings":null},{"chargePointId":"XXXXXXXXXXX","connectorId":2,"type":"Schuko","settings":null}]}]
 
So the above part are working just fine, sensitive information have above been replaced with bogus information. But the issue I have is that I want to do another ‘function’ that handles the login to get a token (there is a login “swagger”on the Charge Amps swagger page) that can be stored in the HC3 as a variable, the  “tokens" are only valid for 2 hours and need to be regenerated several times each day to keep the QA working.
 
To be able to do this you need to provide apiKey, email & password, but I can’t get that to work….. Doing it on the Charge Amps swagger page works just fine, but need this in a ‘function’ in the QA to be able to automate the collection of ‘token’ :

Curl from the Charge Amps swagger page>

Please login or register to see this code.

Request URL from the Charge Amps swagger page>

Please login or register to see this code.

Response body from the Charge Amps swagger page>

Please login or register to see this code.

Code I have tried:

function QuickApp:CAlogin()
 http = net.HTTPClient()
 local requestBody = 'https://eapi.charge.space/api/v4/chargepoints/owned'
 local APIkey = "xxzzyyxxzzyyxxzzyy=="
 
http:request("https://eapi.charge.space/api/v4/auth/[email protected]&password=aabbccddeeff",{
    options = {
               data = requestBody, 
               method = "POST",
               headers = {
                          ["Content-Type"] = "application/json",
                          ["accept"] = "*/*",
                          ["apiKey"] = APIkey
                          }
              },
              success = function(response) 
                        print("Status", response.status)
                        print("Data", response.data)
                        end,
              error = function(message)
                      print("error:", message)
                      end
                }) 
end
 
This simply doesn’t  work and just gives a 400 as status code in the Fibaro log. But how do I do a net.HTTPClient() request that includes apiKey, email and password as required by Charge Amps? Tried to put email and password in the URL but that does not seem to work. Have also tried to put it in other parts of the code but it simply doesn’t work, always gives me a 400 status code.
Edited by JcBorgs

Recommended Posts

  • 0
  • Inquirer
  • Posted
    15 hours ago, Håkan Enhager said:

    The updates version seem to work well!

     

    One thing I noticed which differs from native ChargeAmp app is that after I change the "current limit" I must either turn it off or turn it on in the QA to have the "current limit" to be updated. Easy to compensate for by additional step in a scene. 

    Yes, in the current design the change of current are only executed once the Charger is either turned ON or OFF.  Might change that in a future version.

    4 hours ago, Håkan Enhager said:

    Another observation, still manageable, but the names of the buttons trying to trigger them from a scene are the same, several buttons called ON, OFF, +, - etc.  Since I know in what order they are, it is not a problem.  

    Please login or register to see this link.

     

    Good observation! Haven’t thought about it since I didn’t think of using it from “outside” the QA.

    This will be changed in a coming version, thanks for the feedback!

    • 0
  • Inquirer
  • Posted

    @Håkan Enhager

    A new version will be published this week with support for the new Tibber Monitor QA. But in the future please refer to this thread for updates and questions about this Charge Amps QA:

     

     

    • Like 1

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...