HTTP Requests with `maxon` API



  • Hi,

    As mentioned in this thread I'm looking to bring JSON data into Cinema 4D via HTTP GET request. I noticed in the C++ docs that there's a NetworkHttpHandlerInterface Class. I also noticed in the python docs that the maxon API implements a url type.

    What's the state of these implementations and can they be used to make a connection to an HTTP server and request data? I think I've managed to successfully connect to a remote server, but I can't figure out how to read the data or make more specific requests.

    """Name-en-US: Simple HTTP Requests
    Description-en-US: Prints the result of connecting to wikipedia.org
    """
    
    import c4d
    from c4d import gui
    
    import maxon
    
    def main():
        url = maxon.Url("https://www.wikipedia.org/")
        connection = url.OpenConnection()
        print connection
    
    
    if __name__=='__main__':
        main()
    

    If it's possible, could someone share some example code for how to use the maxon API to print the results of an HTTP GET request in python. If it's not yet possible, may I request that it be added to the SDK?
    Thank you,

    Donovan



  • Hi,

    I am getting a lot of NotImplementedError when I start poking around, which suggests that things are heavily on the NYI side regarding URLs and StreamInterfaces. I also don't think that your code does establish a connection as you said. You instantiate a connection interface but I could not see any traffic from or to the given URL.

    May I also ask why you are not using pythons own urllib? While its reputation is (rightfully) somewhat bad, it certainly will be enough to fetch some JSON.

    Cheers
    zipit



  • Hi @dskeithbuck thanks a lot for trying to use the new Maxon API.
    Unfortunately, the Python Maxon API is not completely ready and still, a lot of things need to be done.

    While it's somehow possible to read data, due to Python Maxon API specific bugs: HTTPS does not support InputStreamRef.GetStreamLength, InputStreamRef.Read/Write doesn't work.
    So even reading currently is not the most intuitive way so I share it, but consider there are actually some bugs/Not yet implemented features that prevents to have the same code than the one used in the C++ URL manual.

    import maxon
    
    def main():
        # Defines the URL
        url = maxon.Url("https://www.wikipedia.org/")
        
        # Creates an Input Stream (you are going to read)
        inputStream = url.OpenInputStream(maxon.OPENSTREAMFLAGS.NONE)
        
        # Creates a BaseArray or char that will received all the needed content
        data = maxon.BaseArray(maxon.Char)
        data.Resize(100)
        
        # Write the data into data
        inputStream.ReadEOS(data)
        
        # Converts the maxon.BaseArray(maxon.Char) to a list to then convert it to a string
        s = "".join(list(data))
        print s
    
    if __name__=='__main__':
        main()
    

    So yes for the moment the best way is to keep using the python standard module. As you want to use some webstuff I also want to mention you do not forget about SSL Error in Mac.

    If you have any question, let me know.

    And thanks again for poking around with the maxon API in python.
    Cheers,
    Maxime.



  • @zipit said in HTTP Requests with `maxon` API:

    I also don't think that your code does establish a connection as you said.

    Perhaps you're right, I tried a bunch of different things before landing here. With one of my tests I was getting a 404 error which implied that network requests were being made.

    May I also ask why you are not using pythons own urllib? While its reputation is (rightfully) somewhat bad, it certainly will be enough to fetch some JSON.

    I'm connecting to a localhost server being run by another application. As I understand it urllib creates a brand new HTTP connection every time I pass data back and forth which is leading to some unfortunate latency. The requests library can make a connection that stays open and all future transactions are much faster.

    I'm hoping to install this plugin cross-platform on a large number of machines and I want to avoid trying to package requests and all of its dependencies if possible which is why I was excited to see that the maxon API seemed to have some of the same abilities as requests.
    Thanks for taking a look!



  • @m_adam said in HTTP Requests with `maxon` API:

    Hi @dskeithbuck thanks a lot for trying to use the new Maxon API.

    Thank you so much for the working sample code, that answers my question. I look forward to switching over to the Maxon API for URL requests once these kinks get worked out.


Log in to reply