saviorand / lightbug_http

Simple and fast HTTP framework for Mojo! 🔥
MIT License
394 stars 27 forks source link

Running the PythonClient Throws a Compilation Error #41

Open saviorand opened 1 month ago

saviorand commented 1 month ago

As reported by Logan logan100 on Discord:

Describe the bug An error is thrown when trying to run the PythonClient, sending a request to httpbin:

➜  MojoTesting git:(main) ✗ mojo main.🔥
/Users/logan/Documents/MojoTesting/lightbug_http.📦:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@http::@HTTPRequest::@"__del__(lightbug_http::http::HTTPRequest)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@http::@HTTPRequest, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.📦:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@uri::@URI::@"__del__(lightbug_http::uri::URI)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@uri::@URI, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.📦:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@python::@client::@PythonClient::@"__del__(lightbug_http::python::client::PythonClient)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@python::@client::@PythonClient, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.📦:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@header::@RequestHeader::@"__del__(lightbug_http::header::RequestHeader)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@header::@RequestHeader, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>

To Reproduce Run the client like below

from lightbug_http.http import HTTPRequest
from lightbug_http.uri import URI
from lightbug_http.python.client import PythonClient
from lightbug_http.header import RequestHeader

fn test_request_simple_url(inout client: PythonClient) raises -> None:
    """
    Test making a simple GET request without parameters.
    Validate that we get a 200 OK response.
    """
    var uri = URI("http://httpbin.org/status/200")
    var request = HTTPRequest(uri)
    var response = client.do(request)
    print(response.header.status_code())

fn main() raises -> None:
    var client = PythonClient()
    test_request_simple_url(client)

Expected behavior The client should make a successful request to the endpoint specified and receive a response.

saviorand commented 1 month ago

I think there was a regression on main after #40 , I should definitely add a proper test suite to run in the CI to know when this happens...

The code below, similar to how you tried it before, should now work. Don't forget to pull the latest changes. Will add the example code for running the client to the README.

PythonClient is still throwing compile errors, but this is related to another issue -- If MojoClient works for you I'll close those in favor of #34 . Let me know if you have any questions!

from lightbug_http.http import HTTPRequest
from lightbug_http.uri import URI
from lightbug_http.sys.client import MojoClient

fn test_request_simple_url(inout client: MojoClient) raises -> None:
    """
    Test making a simple GET request without parameters.
    Validate that we get a 200 OK response.
    """
    var uri = URI("http://httpbin.org/status/200")
    var request = HTTPRequest(uri)
    var response = client.do(request)

    # print status code
    print(response.header.status_code())

    # print various parsed headers
    print(response.header.content_length())

    # print body
    # print(String(response.get_body()))

fn main() raises -> None:
    var client = MojoClient()
    test_request_simple_url(client)
Logan9312 commented 1 month ago

I tried running this code, but it's behaving very strange. This is using the pkg file from the latest release

from lightbug_http.http import HTTPRequest from lightbug_http.uri import URI from lightbug_http.sys.client import MojoClient

fn test_request_simple_url(inout client: MojoClient) raises -> None:
    """
    Test making a simple GET request without parameters.
    Validate that we get a 200 OK response.
    """
    var uri = URI("http://httpbin.org/status/200")
    var request = HTTPRequest(uri)
    var response = client.do(request)

    # print status code
    print("Response:", response.header.status_code())

    # print various parsed headers
    print("Header", response.header.content_length())

    # print body
    print(String(response.get_body()))

fn main() raises -> None:
    var client = MojoClient()
    print("Testing URL request")
    test_request_simple_url(client)
    print("Done")

This is the output I get:

mojo main.🔥
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@http::@HTTPRequest::@"__del__(lightbug_http::http::HTTPRequest)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@http::@HTTPRequest, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@uri::@URI::@"__del__(lightbug_http::uri::URI)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@uri::@URI, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@sys::@client::@MojoClient::@"__del__(lightbug_http::sys::client::MojoClient)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@sys::@client::@MojoClient, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
Testing URL request
Done

It's not even printing anything for some reason in the test function

saviorand commented 1 month ago

@Logan9312 hmm I see, cannot reproduce this one on my Mac M1 using this latest package. Will try on a Linux machine later today.

saviorand commented 1 month ago

@Logan9312 hmm, couldn't reproduce on Linux as well. Could you share more details about your setup? Can you maybe try removing everything and creating a clean setup with the latest package + code you shared? Will appreciate anything that can help reproduce this. Thanks!

saviorand commented 1 month ago

Added a section in the README on using the client, see here: https://github.com/saviorand/lightbug_http?tab=readme-ov-file#using-the-client

Logan9312 commented 1 month ago

@Logan9312 hmm I see, cannot reproduce this one on my Mac M1 using this latest package. Will try on a Linux machine later today.

Using the package you linked here, I downloaded it and put it into the top level alongside a main.🔥 file.

The main file only contained the code you put in the README:

from lightbug_http.http import HTTPRequest
from lightbug_http.uri import URI
from lightbug_http.sys.client import MojoClient

fn test_request(inout client: MojoClient) raises -> None:
    var uri = URI("http://httpbin.org/")
    var request = HTTPRequest(uri)
    var response = client.do(request)

    # print status code
    print("Response:", response.header.status_code())

    # print various parsed headers
    print("Header", response.header.content_length())

    # print body
    print(String(response.get_body()))

fn main() raises -> None:
    var client = MojoClient()
    test_request(client)

My setup is on an M2 Macbook air. Mojo version is mojo 2024.5.1905 (46b7e7ee).

When running by doing mojo main.🔥 I get the following response:

➜  MojoTesting mojo main.🔥
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@http::@HTTPRequest::@"__del__(lightbug_http::http::HTTPRequest)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@http::@HTTPRequest, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@uri::@URI::@"__del__(lightbug_http::uri::URI)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@uri::@URI, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected mlir::StringAttr, but got: #kgen.symbol.constant<@lightbug_http::@sys::@client::@MojoClient::@"__del__(lightbug_http::sys::client::MojoClient)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@sys::@client::@MojoClient, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
➜  MojoTesting 
saviorand commented 1 month ago

Ah, I see. Thanks for the details. Was able to reproduce now after switching to the nightly version. I'll try to debug but in the meantime switching to the latest stable version (24.3) should help.

Logan9312 commented 1 month ago

I got that version when running modular install mojo so I assumed it was the latest stable. When I run modular install nightly/mojo I get version mojo 2024.5.2514 (f472b8ac)

saviorand commented 1 month ago

@Logan9312 Can you try with this version? https://github.com/saviorand/lightbug_http/releases/tag/nightly-2805 Works for me now on the nightly. The PR is here: https://github.com/saviorand/lightbug_http/pull/51 , will merge after this nightly turns into a release version, so it doesn't break for folks on the current release version

Logan9312 commented 1 month ago

@saviorand Sorry for late response, but I tried that package and still get the following errors:

/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected M::KGEN::LIT::DocStringAttr, but got: #kgen.symbol.constant<@lightbug_http::@http::@HTTPRequest::@"__del__(lightbug_http::http::HTTPRequest)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@http::@HTTPRequest, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected M::KGEN::LIT::DocStringAttr, but got: #kgen.symbol.constant<@lightbug_http::@uri::@URI::@"__del__(lightbug_http::uri::URI)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@uri::@URI, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>
/Users/logan/Documents/MojoTesting/lightbug_http.mojopkg:0:0: error: expected M::KGEN::LIT::DocStringAttr, but got: #kgen.symbol.constant<@lightbug_http::@sys::@client::@MojoClient::@"__del__(lightbug_http::sys::client::MojoClient)"> : !kgen.signature<!lit.signature<[1]("self": !lit.ref<@lightbug_http::@sys::@client::@MojoClient, mut *[0,0]> owned_in_mem, |) -> !kgen.none>>

Seems like it doesn't even print the body. I'm wondering if this is maybe just a bug with mojo itself independent of the library?