Skip to content

Commit 0814562

Browse files
committed
update readme and client example
1 parent 0e79892 commit 0814562

File tree

5 files changed

+53
-54
lines changed

5 files changed

+53
-54
lines changed

README.md

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ Once you have a Mojo project set up locally,
8888
@value
8989
struct Printer(HTTPService):
9090
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
91-
var uri = req.uri()
92-
print("Request URI: ", to_string(uri.request_uri()))
93-
94-
var header = req.header
95-
print("Request protocol: ", header.protocol_str())
96-
print("Request method: ", to_string(header.method()))
97-
print("Request Content-Type: ", to_string(header.content_type()))
91+
var uri = req.uri
92+
print("Request URI: ", to_string(uri.request_uri))
93+
94+
var header = req.headers
95+
print("Request protocol: ", req.protocol)
96+
print("Request method: ", req.method)
97+
print(
98+
"Request Content-Type: ", to_string(header[HeaderKey.CONTENT_TYPE])
99+
)
98100
99101
var body = req.body_raw
100102
print("Request Body: ", to_string(body))
@@ -103,9 +105,11 @@ Once you have a Mojo project set up locally,
103105
```
104106
6. Start a server listening on a port with your service like so.
105107
```mojo
108+
from lightbug_http import Welcome, SysServer
109+
106110
fn main() raises:
107111
var server = SysServer()
108-
var handler = Printer()
112+
var handler = Welcome()
109113
server.listen_and_serve("0.0.0.0:8080", handler)
110114
```
111115
Feel free to change the settings in `listen_and_serve()` to serve on a particular host and port.
@@ -123,15 +127,15 @@ from lightbug_http import *
123127
struct ExampleRouter(HTTPService):
124128
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
125129
var body = req.body_raw
126-
var uri = req.uri()
130+
var uri = req.uri
127131
128-
if uri.path() == "/":
132+
if uri.path == "/":
129133
print("I'm on the index path!")
130-
if uri.path() == "/first":
134+
if uri.path == "/first":
131135
print("I'm on /first!")
132-
elif uri.path() == "/second":
136+
elif uri.path == "/second":
133137
print("I'm on /second!")
134-
elif uri.path() == "/echo":
138+
elif uri.path == "/echo":
135139
print(to_string(body))
136140
137141
return OK(body)
@@ -152,21 +156,21 @@ from lightbug_http import *
152156
@value
153157
struct Welcome(HTTPService):
154158
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
155-
var uri = req.uri()
159+
var uri = req.uri
156160
157-
if uri.path() == "/":
161+
if uri.path == "/":
158162
var html: Bytes
159163
with open("static/lightbug_welcome.html", "r") as f:
160164
html = f.read_bytes()
161165
return OK(html, "text/html; charset=utf-8")
162-
163-
if uri.path() == "/logo.png":
166+
167+
if uri.path == "/logo.png":
164168
var image: Bytes
165169
with open("static/logo.png", "r") as f:
166170
image = f.read_bytes()
167171
return OK(image, "image/png")
168-
169-
return NotFound(uri.path())
172+
173+
return NotFound(uri.path)
170174
```
171175

172176
### Using the client
@@ -178,33 +182,34 @@ from lightbug_http import *
178182
from lightbug_http.sys.client import MojoClient
179183
180184
fn test_request(inout client: MojoClient) raises -> None:
181-
var uri = URI("http://httpbin.org/status/404")
182-
try:
183-
uri.parse()
184-
except e:
185-
print("error parsing uri: " + e.__str__())
186-
185+
var uri = URI.parse_raises("http://httpbin.org/status/404")
186+
var headers = Header("Host", "httpbin.org")
187187
188-
var request = HTTPRequest(uri)
189-
var response = client.do(request)
188+
var request = HTTPRequest(uri, headers)
189+
var response = client.do(request^)
190190
191191
# print status code
192-
print("Response:", response.header.status_code())
192+
print("Response:", response.status_code)
193193
194194
# print parsed headers (only some are parsed for now)
195-
print("Content-Type:", to_string(response.header.content_type()))
196-
print("Content-Length", response.header.content_length())
197-
print("Server:", to_string(response.header.server()))
195+
print("Content-Type:", response.headers["Content-Type"])
196+
print("Content-Length", response.headers["Content-Length"])
197+
print("Server:", to_string(response.headers["Server"]))
198198
199-
print("Is connection set to connection-close? ", response.header.connection_close())
199+
print(
200+
"Is connection set to connection-close? ", response.connection_close()
201+
)
200202
201203
# print body
202-
print(to_string(response.get_body_bytes()))
204+
print(to_string(response.body_raw))
203205
204206
205-
fn main() raises -> None:
206-
var client = MojoClient()
207-
test_request(client)
207+
fn main() -> None:
208+
try:
209+
var client = MojoClient()
210+
test_request(client)
211+
except e:
212+
print(e)
208213
```
209214

210215
Pure Mojo-based client is available by default. This client is also used internally for testing the server.

client.mojo

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ from lightbug_http.sys.client import MojoClient
44

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

8-
var request = HTTPRequest(uri)
9+
var request = HTTPRequest(uri, headers)
910
var response = client.do(request^)
1011

1112
# print status code
@@ -24,6 +25,9 @@ fn test_request(inout client: MojoClient) raises -> None:
2425
print(to_string(response.body_raw))
2526

2627

27-
fn main() raises -> None:
28-
var client = MojoClient()
29-
test_request(client)
28+
fn main() -> None:
29+
try:
30+
var client = MojoClient()
31+
test_request(client)
32+
except e:
33+
print(e)

lightbug_http/__init__.mojo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from lightbug_http.http import HTTPRequest, HTTPResponse, OK, NotFound
22
from lightbug_http.uri import URI
3+
from lightbug_http.header import Header, Headers, HeaderKey
34
from lightbug_http.service import HTTPService, Welcome
45
from lightbug_http.sys.server import SysServer
56
from lightbug_http.strings import to_string

lightbug_http/uri.mojo

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,9 @@ struct URI:
4242
u._parse()
4343
return u
4444

45-
fn __init__(inout self) -> None:
46-
self.__path_original = "/"
47-
self.scheme = ""
48-
self.path = "/"
49-
self.query_string = ""
50-
self.__hash = ""
51-
self.host = ""
52-
self.full_uri = ""
53-
self.request_uri = ""
54-
self.username = ""
55-
self.password = ""
56-
5745
fn __init__(
5846
inout self,
59-
uri: String
47+
uri: String = "",
6048
) -> None:
6149
self.__path_original = "/"
6250
self.scheme =

tests/test_client.mojo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ fn test_mojo_client_lightbug_external_req(client: MojoClient) raises:
2121
Header("Connection", "keep-alive"),
2222
Header("Host", "httpbin.org")),
2323
method="GET",
24+
protocol="GET",
2425
)
25-
26+
2627
try:
2728
var res = client.do(req)
2829
testing.assert_equal(res.status_code, 200)

0 commit comments

Comments
 (0)