Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers import Errors #62

Open
dasTholo opened this issue Sep 21, 2024 · 2 comments
Open

Headers import Errors #62

dasTholo opened this issue Sep 21, 2024 · 2 comments

Comments

@dasTholo
Copy link

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

  • OS: Arch Linux
  • magic --version magic 0.3.0
  • mojo --version mojo 24.5.0 (e8aacb95)
  • mojoproject.toml
 [project]
authors = ["dasTholo <[email protected]>"]
channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/mojo-community"]
description = "Add a short description here"
name = "arango-mojo"
platforms = ["linux-64"]
version = "0.1.0"

[tasks]

[dependencies]
max = ">=24.5.0,<25"
lightbug_http = ">=0.1.3,<0.2"
@saviorand
Copy link
Owner

saviorand commented Sep 21, 2024

@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
Copy link
Author

dasTholo commented Sep 21, 2024

Thank you very much!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants