{"id":353012,"date":"2024-05-20T22:19:33","date_gmt":"2024-05-20T22:19:33","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=353012"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=353012","title":{"rendered":"<span>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 RESTful API \u043d\u0430 Python \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HappyX<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043e \u044d\u0442\u043e\u0433\u043e \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0435\u0431 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 <a href=\"https:\/\/nim-lang.org\" rel=\"nofollow noopener noreferrer\">Nim<\/a> <a href=\"https:\/\/habr.com\/ru\/articles\/748624\/\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/753634\/\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<blockquote><p>\u0427\u0442\u043e? \u041a\u0430\u043a\u043e\u0439 Nim? \u0412 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0443\u043a\u0430\u0437\u0430\u043d Python!<\/p><\/blockquote>\n<p>\u0414\u0430, \u0434\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0435\u0431 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 <a href=\"https:\/\/github.com\/HapticX\/happyx\" rel=\"nofollow noopener noreferrer\">\u0432\u0435\u0431 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 HappyX<\/a> <strong>\u0431\u044b\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e<\/strong> \u043b\u0438\u0448\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Nim. \u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 HappyX <strong>\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u043d\u0430 Python<\/strong>. \u041b\u044e\u0431\u043e\u0439 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 Nim.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/hg\/zy\/ki\/hgzykiepb0zrndi_lvzwitcsi6u.png\" alt=\"\u0432\u0435\u0431 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a HappyX\" data-src=\"https:\/\/habrastorage.org\/webt\/hg\/zy\/ki\/hgzykiepb0zrndi_lvzwitcsi6u.png\"\/><\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <strong>\u0444\u0435\u0439\u043a\u043e\u0432\u043e\u0435<\/strong> API GitHub&#8217;\u0430.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"sozdanie-proekta\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u041f\u0440\u043e\u0435\u043a\u0442\u0430<\/h2>\n<p>  <\/p>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 HappyX \u0432 Python \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 CLI, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c <del>\u043b\u0430\u043f\u043a\u0430\u043c\u0438<\/del> \u0440\u0443\u0447\u043a\u0430\u043c\u0438. \u0425\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e PyCharm \u2014 \u044d\u0442\u043e \u043c\u044b \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/rz\/oc\/na\/rzocnazqwq_tuf5njftpkmhycno.png\" alt=\"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432 PyCharm\" data-src=\"https:\/\/habrastorage.org\/webt\/rz\/oc\/na\/rzocnazqwq_tuf5njftpkmhycno.png\"\/><\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c <strong>\u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c<\/strong> \u0441\u0430\u043c\u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>happyx<\/code>. \u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f Python \u0432\u0435\u0440\u0441\u0438\u0438 <code>3.10<\/code> \u0438 \u0432\u044b\u0448\u0435.<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">pip install happyx<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u2014 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>new_server<\/code> \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from happyx import Server, JsonResponse<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 <strong>\u0444\u0435\u0439\u043a\u043e\u0432\u0443\u044e<\/strong> \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">app = Server('127.0.0.1', 5000) github_repos = [     {'user': 'HapticX', 'repo': 'happyx', 'stars_count': 1, 'description': 'web framework'},     {'user': 'python273', 'repo': 'vk_api', 'stars_count': 10, 'description': 'VK API for python'},     {'user': 'pydantic', 'repo': 'pydantic', 'stars_count': 5, 'description': 'python models'}, ]<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u0441\u0430\u043c\u0438 \u044d\u043d\u0434\u043f\u043e\u0439\u043d\u0442\u044b.<\/p>\n<p>  <\/p>\n<p><strong>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"python\">@app.get('\/repos') def handle():     return github_repos<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043f\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044e<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"python\">@app.get('\/{user}\/{repo}') def get_repo(user: str, repo: str):     for r in github_repos:         if r['user'].lower() == user.lower() and r['repo'].lower() == repo.lower():             return r     return JsonResponse(         {'response': 'failure'},         status_code=404     )<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <strong>\u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/strong>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     app.start()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">py main.py<\/code><\/pre>\n<p>  <\/p>\n<p>\u041b\u0438\u0431\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0447\u0435\u0440\u0435\u0437 PyCharm.<\/p>\n<p>  <\/p>\n<blockquote><p>\u0412\u0441\u0435 \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u043e?<\/p><\/blockquote>\n<p>\u0414\u0430, HappyX \u043f\u0440\u043e\u0441\u0442 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u2014 \u0447\u0442\u043e \u043d\u0430 Python, \u0447\u0442\u043e \u043d\u0430 Nim.<\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u043a \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u043c\u0443<\/strong><br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u0438\u043c\u043f\u043e\u0440\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from happyx import new_server, JsonResponse, RequestModelBase<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <strong>\u043c\u043e\u0434\u0435\u043b\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Repository(RequestModelBase):     name: str     owner: str     description: str<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0441\u0430\u043c \u043c\u0435\u0442\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">@app.post('\/repo[repo]') def create_repo(repo: Repository):     if repo.name == '' or repo.owner == '':         return JsonResponse(             {'response': 'failure', 'msg': 'repo should have name and owner!'},             status_code=404         )     github_repos.append({         'repo': repo.name, 'user': repo.owner,         'stars_count': 0, 'description': repo.description     })     return JsonResponse({'response': 'success'})<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 \ud83d\ude42<br \/>  \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0432 postman.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u0448\u0435 API GitHub <strong>\u043d\u0430 \u043a\u043e\u043b\u0435\u043d\u043a\u0435<\/strong> \u0441\u043e\u0437\u0434\u0430\u043d\u043e!<\/p>\n<p>  <\/p>\n<p>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0431\u0430\u0433 \u0438 \u043e\u0442\u043a\u0440\u043e\u0435\u0442\u0435 <strong>issue<\/strong> \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438.<br \/>  \u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044e \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f \ud83d\ude09<\/p>\n<p>  <\/p>\n<p>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434: <a href=\"https:\/\/github.com\/HapticX\/happyx\" rel=\"nofollow noopener noreferrer\">HapticX\/happyx<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/756548\/\"> https:\/\/habr.com\/ru\/articles\/756548\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043e \u044d\u0442\u043e\u0433\u043e \u044f \u043f\u0438\u0441\u0430\u043b \u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0435\u0431 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 <a href=\"https:\/\/nim-lang.org\" rel=\"nofollow noopener noreferrer\">Nim<\/a> <a href=\"https:\/\/habr.com\/ru\/articles\/748624\/\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/753634\/\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<blockquote><p>\u0427\u0442\u043e? \u041a\u0430\u043a\u043e\u0439 Nim? \u0412 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0443\u043a\u0430\u0437\u0430\u043d Python!<\/p><\/blockquote>\n<p>\u0414\u0430, \u0434\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0435\u0431 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 <a href=\"https:\/\/github.com\/HapticX\/happyx\" rel=\"nofollow noopener noreferrer\">\u0432\u0435\u0431 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 HappyX<\/a> <strong>\u0431\u044b\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e<\/strong> \u043b\u0438\u0448\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Nim. \u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 HappyX <strong>\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438 \u043d\u0430 Python<\/strong>. \u041b\u044e\u0431\u043e\u0439 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 Nim.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/hg\/zy\/ki\/hgzykiepb0zrndi_lvzwitcsi6u.png\" alt=\"\u0432\u0435\u0431 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a HappyX\" data-src=\"https:\/\/habrastorage.org\/webt\/hg\/zy\/ki\/hgzykiepb0zrndi_lvzwitcsi6u.png\"\/><\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <strong>\u0444\u0435\u0439\u043a\u043e\u0432\u043e\u0435<\/strong> API GitHub&#8217;\u0430.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-353012","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/353012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353012"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/353012\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}