1
1
import requests
2
+ from config import PORT
2
3
3
- url = f"http://localhost:5000"
4
-
5
- # ping = requests.get(f'{url}/ping')
6
- # print(ping.text)
7
-
8
- payload = {
9
- 'id' : "1" ,
10
- 'question' : 'hello world'
11
- }
12
-
13
- headers = {
14
- 'Content-Type' : 'application/json'
15
- }
16
-
17
- def get_stream (url ):
18
- s = requests .Session ()
19
- with s .post (url , headers = headers , json = payload , stream = True ) as resp :
20
- # with s.post(url, headers={'Content-Type': 'application/json'}, stream=True) as resp:
21
- for line in resp .iter_lines ():
22
- if line :
23
- print ("triggered => " )
24
- yield line .decode ('utf-8' )
25
-
26
- url = f'{ url } /questions/stream-generator'
27
- # url = 'https://jsonplaceholder.typicode.com/posts/1'
28
- for line in get_stream (url ):
29
- print (line )
4
+ URL = f"http://localhost:{ PORT } "
5
+
6
+ def get_ping ():
7
+ ping = requests .get (f'{ URL } /ping' )
8
+ print (ping .text )
9
+
10
+ def post_stream_generator ():
11
+ payload = {
12
+ 'id' : "1" ,
13
+ 'question' : 'hello world'
14
+ }
15
+
16
+ headers = {
17
+ 'Content-Type' : 'application/json'
18
+ }
19
+
20
+ def get_stream (url ):
21
+ s = requests .Session ()
22
+ with s .post (url , headers = headers , json = payload , stream = True ) as resp :
23
+ # with s.post(url, headers={'Content-Type': 'application/json'}, stream=True) as resp:
24
+ for line in resp .iter_lines ():
25
+ if line :
26
+ print ("triggered => " )
27
+ yield line .decode ('utf-8' )
28
+
29
+ url = f'{ URL } /questions/stream-generator'
30
+ # url = 'https://jsonplaceholder.typicode.com/posts/1'
31
+ data_rcv = ''
32
+ for line in get_stream (url ):
33
+ data_rcv += line [6 :]
34
+ print (f"{ data_rcv } \n " )
35
+
36
+ def post_questions (question ):
37
+ payload = {
38
+ 'id' : "1" ,
39
+ 'question' : question
40
+ }
41
+
42
+ headers = {
43
+ 'Content-Type' : 'application/json'
44
+ }
45
+
46
+ def get_stream (url ):
47
+ s = requests .Session ()
48
+ with s .post (url , headers = headers , json = payload , stream = True ) as resp :
49
+ # with s.post(url, headers={'Content-Type': 'application/json'}, stream=True) as resp:
50
+ for line in resp .iter_lines ():
51
+ if line :
52
+ print ("triggered => " )
53
+ yield line .decode ('utf-8' )
54
+
55
+ url = f'{ URL } /questions'
56
+ # url = 'https://jsonplaceholder.typicode.com/posts/1'
57
+ data_rcv = ''
58
+ for line in get_stream (url ):
59
+ data_rcv += line [6 :]
60
+ print (f"{ data_rcv } \n " )
61
+
62
+ # get_ping()
63
+ # post_stream_generator()
64
+ post_questions ("bagaimana Pengambilan MK non-Konversi di semester yang sama dengan pengambilan MK Konversi? jawab bahasa indonesia" )
0 commit comments