saviorand / lightbug_http

Simple and fast HTTP framework for Mojo! 🔥
MIT License
504 stars 32 forks source link

Headers import Errors #62

Open dasTholo opened 4 hours ago

dasTholo commented 4 hours ago

Describe the bug I would like to test the HttpClient. But both imports will raise Errors, can you explain this import errors? I can´t understand why Headers not init.

I have install lightbug_http via magic at a new project

To Reproduce

Example 1 with import *


from lightbug_http.sys.client import MojoClient
from lightbug_http import *

fn test_request(inout client: MojoClient) raises -> None:
    var uri = URI("http://httpbin.org/status/200")
    var headers = Headers(
        Header("Connection", "keep-alive"),
        Header("Host", "httpbin.org"),
        )
    var request = HTTPRequest(uri,headers)
    var as_str = str(request)
    print(as_str)
    var response = client.do(request^)

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

    # print parsed headers (only some are parsed for now)
    print("Content-Type:", response.headers["Content-Type"])
    print("Content-Length", response.headers["Content-Length"])
    print("Server:", to_string(response.headers["Server"]))

    print(
        "Is connection set to connection-close? ", response.connection_close()
    )

    # print body
    print(to_string(response.body_raw))

fn main() -> None:
    try:
        var client = MojoClient()
        test_request(client)
    except e:
        print(e)

Errors

(arango-mojo) ➜ arango-mojo git:(main) ✗ magic run mojo hello2.mojo /home/tholo/Scripts/mojo/arango-mojo/hello2.mojo:7:19: error: use of unknown declaration 'Headers' var headers = Headers( ^~~ /home/tholo/Scripts/mojo/arango-mojo/.magic/envs/default/bin/mojo: error: failed to parse the provided Mojo source module

Example 2 with import strict imports

from lightbug_http.sys.client import MojoClient
from lightbug_http.http import HTTPRequest
from lightbug_http.uri import URI
from lightbug_http.header import Header, Headers

fn test_request(client: MojoClient) raises:
    var uri = URI("http://httpbin.org/status/200")
    var req = HTTPRequest(
        uri,
        body=String("Hello world!").as_bytes(),
        headers=Headers(
            Header("Connection", "keep-alive"),
            Header("Host", "httpbin.org"))),

    try:
        var response = client.do(req)
        print("Response:", response.status_code)

        # print parsed headers (only some are parsed for now)
        print("Content-Type:", response.headers["Content-Type"])
        print("Content-Length", response.headers["Content-Length"])

        print(
            "Is connection set to connection-close? ", response.connection_close()
        )

        # print body
        #print(to_string(response.body_raw))

    except e:
        print(e)

fn main() -> None:
    try:
        var client = MojoClient()
        test_request(client)
    except e:
        print(e)

Errors

(arango-mojo) ➜ arango-mojo git:(main) ✗ magic run mojo hello.mojo /home/tholo/Scripts/mojo/arango-mojo/hello.mojo:4:34: error: module 'header' does not contain 'Header' from lightbug_http.header import Header, Headers ^ /home/tholo/Scripts/mojo/arango-mojo/hello.mojo:4:42: error: module 'header' does not contain 'Headers' from lightbug_http.header import Header, Headers ^ /home/tholo/Scripts/mojo/arango-mojo/.magic/envs/default/bin/mojo: error: failed to parse the provided Mojo source module

Desktop

[tasks]

[dependencies] max = ">=24.5.0,<25" lightbug_http = ">=0.1.3,<0.2"

saviorand commented 4 hours ago

@dasTholo thanks for the detailed report! This is probably because main got updated yesterday but new package version wasn't published to the prefix channel. Just updated, you can also trying changing [dependencies] to lightbug_http = ">=0.1.4,<0.2". Worked for me on a test VM, let me know if this doesn't solve the problem. Here's an example that works for me:

from lightbug_http import *
from lightbug_http.sys.client import MojoClient

fn test_request(inout client: MojoClient) raises -> None:
    var uri = URI.parse_raises("http://httpbin.org/status/404")
    var headers = Header("Host", "httpbin.org")

    var request = HTTPRequest(uri, headers)
    var response = client.do(request^)

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

    # print parsed headers (only some are parsed for now)
    print("Content-Type:", response.headers["Content-Type"])
    print("Content-Length", response.headers["Content-Length"])
    print("Server:", to_string(response.headers["Server"]))

    print(
        "Is connection set to connection-close? ", response.connection_close()
    )

    # print body
    print(to_string(response.body_raw))

fn main() -> None:
    try:
        var client = MojoClient()
        test_request(client)
    except e:
        print(e)

Interestingly, with your test code I'm getting URI is nil. Will investigate this and likely address in #48

dasTholo commented 1 hour ago

Thank you very much!

Yeah, upgrade will help with the imports! But yes "URI is nil" no result or status codes