Created
April 24, 2018 16:01
-
-
Save Timofffee/f166ec73e84128a705fdb48e7171a9d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://telegra.ph/HTTPHTTPS-zaprosy-v-Godot-Engine-04-24 | |
extends RichTextLabel | |
# var http = preload('Путь до скрипта http_request.gd') | |
var http = preload('res://???/http_request.gd') | |
# Инициализируем | |
onready var req = http.new() | |
func _ready(): | |
text = '' | |
get_post_with_comments(1) | |
func get_post_with_comments(post_id): | |
yield(get_post(1), 'loaded') | |
yield(get_comments(1), 'loaded') | |
func get_post(post_id = 1): | |
var res = req.get('https://jsonplaceholder.typicode.com', # домен | |
'/posts/%s' % post_id, # url | |
443, # порт | |
false, # использование ssl | |
["Content-Type:application/json"]) # тип контента | |
res.connect("loaded",self,"_get_post_loaded", [], CONNECT_ONESHOT) | |
return res | |
func get_comments(post_id = 1): | |
var res = req.get('https://jsonplaceholder.typicode.com', # домен | |
'/posts/%s/comments' % post_id, # url | |
443, # порт | |
false, # использование ssl | |
["Content-Type:application/json"]) # тип контента | |
res.connect("loaded",self,"_get_comments_loaded", [], CONNECT_ONESHOT) | |
return res | |
func _get_post_loaded(r): | |
r = JSON.parse(r).result | |
text += "\n%s\n\n\t%s\n\n\n" % [ | |
r['title'], | |
r['body'] | |
] | |
func _get_comments_loaded(r): | |
r = JSON.parse(r).result | |
text += 'Comments:\n\n' | |
for comment in r: | |
text += "Author: %s\nE-mail:%s\n\nText:\n\t%s\n\n\n" % [ | |
comment['name'], | |
comment['email'], | |
comment['body'] | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment