我正在构建一个Django Rest框架,并希望测试API
with coreapi
库.我可以coreapi
在python脚本中以编程方式创建一个对象,但在命令行中,我无法创建相同的对象,当我列出coreapi
端点时,我得到的列表只包含用于读取和列出的端点,即使我添加了有效的凭据.
我的架构:
{
"_type": "document",
"_meta": {
"url": "http://127.0.0.1:8000/api/schema/",
"title": "NEP API"
},
"experiments": {
"list": {
"_type": "link",
"url": "/api/experiments/",
"action": "get",
"fields": [
{
"name": "page",
"location": "query",
"schema": {
"_type": "integer",
"title": "Page",
"description": "A page number within the paginated result set."
}
}
]
},
"create": {
"_type": "link",
"url": "/api/experiments/",
"action": "post",
"encoding": "application/json",
"fields": [
{
"name": "title",
"required": true,
"location": "form",
"schema": {
"_type": …
Run Code Online (Sandbox Code Playgroud) 根据DRF文档,我开始使用ViewSet并实施了list, retrieve, create, update and destroy
操作.我有另一个APIView,我能够编写模式(ManualSchema),当我导航到/docs/
我能够文档以及实时端点进行交互时.
我希望为每个视图集操作创建单独的模式.我尝试写一个,但它没有出现,所以我想我错过了一些东西.
这是代码:
class Clients(viewsets.ViewSet):
'''
Clients is DRF viewset which implements `create`, `update`, `read` actions by implementing create, update, list and retrieve functions respectively.
'''
list_schema = schemas.ManualSchema(fields=[
coreapi.Field(
'status',
required=False,
location='query',
description='Accepted values are `active`, `inactive`'
),
],
description='Clients list',
encoding='application/x-www-form-urlencoded')
@action(detail=True, schema=list_schema)
def list(self, request):
'''Logic for listing'''
def retrieve(self, request, oid=None):
'''Logic for retrieval'''
create_schema = schemas.ManualSchema(fields=[
coreapi.Field(
'name',
required=False,
location='body',
),
coreapi.Field(
'location',
required=False,
location='body',
), …
Run Code Online (Sandbox Code Playgroud) 我在Django Rest Framework中创建可自定义的swagger模式时遇到问题.我已经阅读了文档页面,但没有找到关于如何在python中生成swagger注释的明确示例.
我知道在Django中使用ViewSets时很容易生成swagger/schema文档.但是,我只使用APIViews并希望编写自定义架构.我尝试过创建一个CoreAPI架构,但我不知道如何实现它.我附上了一些示例代码和一些屏幕截图.屏幕截图从我拥有的到我想要的.
示例代码:
urls.py
from django.conf.urls import url, include
from rest_framework.urlpatterns import format_suffix_patterns
from Views import SampleView as sv
from rest_framework_swagger.views import get_swagger_view
from rest_framework.documentation import include_docs_urls
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.schemas import get_schema_view
schema_view enter code here= get_swagger_view(
title='Sample API')
urlpatterns = [
url(r'^sample/$', pv.SampleList.as_view()),
url(r'^sample/(?P<id>[a-f\d]{24})/$', sv.SampleDetail.as_view()),
url('^schema/$', schema_view),
]
urlpatterns = format_suffix_patterns(urlpatterns)
Run Code Online (Sandbox Code Playgroud)
views.py
from rest_framework.views import APIView
from Manager.SampleManager import SampleManager as sm
_sampleManager = sm()
class SampleList(APIView):
"""
get:
Return a list of all …
Run Code Online (Sandbox Code Playgroud) core-api mongoengine python-3.x swagger django-rest-framework
我正在为使用Django Rest Framework和Django 1.11构建的Web API编写一些集成测试。当我使用DRF的APIClient将请求发送到我的API端点时,我的测试通过了,但是当我使用CoreAPIClient时,请求引发了异常并返回内部服务器错误。但是,如果我使用Postman,甚至使用coreapi-cli命令,它都可以正常工作。
我的测试代码:
这有效:
from rest_framework.test import APITestCase, APIClient
self.client = APIClient()
account_params = {
'email': 'test1@example.com',
'username': 'testuser1',
'password': 'test1password'
}
account_response = self.client.post(reverse('account-list'), data=account_params)
Run Code Online (Sandbox Code Playgroud)
但是使用核心api客户端不起作用:
from rest_framework.test import APITestCase, CoreAPIClient
self.client = CoreAPIClient()
self.schema = self.client.get('http://localhost/api/schema')
params = {
'email': 'test3@example.com',
'username': 'testuser3',
'password': 'test3password'
}
self.test_user = self.client.action(self.schema, ['accounts', 'create'], params=params)
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
Error
Traceback (most recent call last):
File "/home/pitt/dev/cps/cps_mono/surveys/tests.py", line 429, in setUp
self.test_user = self.client.action(self.schema, ['accounts', 'create'], params=params)
File "/home/pitt/.virtualenvs/cps_mono/lib/python3.5/site-packages/coreapi/client.py", line 178, …
Run Code Online (Sandbox Code Playgroud) 我如何让微软了解我在其核心库例程中发现的问题?他们是否有一个中央存储库来报告这些事情?
我不是Microsoft Development Network(MSDN)的成员.
或者我应该打扰?
所以我安装了django-rest-swagger,如django rest文档中所示.
而且
from django.conf.urls import url
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='Pastebin API')
urlpatterns = [
url(r'^$', schema_view)
]
Run Code Online (Sandbox Code Playgroud)
我继续得到以下错误,
文件".../local/lib/python2.7/site-packages/django_filters/rest_framework/backends.py",第97行,在get_schema_fields断言中compat.coreapi不是None,'必须安装coreapi才能使用
get_schema_fields()
'AssertionError:必须安装coreapi才能使用get_schema_fields()
我安装了以下软件包:
编辑:
已安装的应用:
首先,我是 docker 容器的新手,我不太了解它们是如何运行的,但我有这样的设置:
该 API 可通过https://localhost:44384/api/weatherforecast 访问 容器化角度应用程序可通过https://localhost:4200访问
如果我在没有 docker 的情况下运行 Angular 应用程序,则在使用代理修复 CORS 问题后,我可以连接到https://localhost:44384/api/weatherforecast。但是,当我运行 dockerized 版本时,我收到以下错误:
Failed to load resource: the server responded with a status of 504 (Gateway Timeout)
Run Code Online (Sandbox Code Playgroud)
在 VS 控制台中我看到以下错误:
clearance_1 | [HPM] Error occurred while trying to proxy request /api/weatherforecast from loccoalhost:4200 to https://localhost:44384 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
Run Code Online (Sandbox Code Playgroud)
这似乎是一个连接问题,所以我在互联网上进行了一些挖掘,并尝试将这两个容器置于同一网络下。
步骤如下: 1. 创建桥接“测试”网络
docker network create test …
Run Code Online (Sandbox Code Playgroud) 我正在使用django的rest_framework库为django应用程序构建API.一切都很顺利,我可以通过curl命令按预期访问我的API.
现在,我想使用CoreAPI以客户端库的形式使事情变得更加优雅.
我可以进行如下基本身份验证:
auth = coreapi.auth.BasicAuthentication(username=user, password=password)
client = coreapi.Client(auth=auth)
Run Code Online (Sandbox Code Playgroud)
我能够很好地访问API的架构.
但是,我想使用我的令牌身份验证(通过rest_framework.tokenauthenticaiton)(通过curl工作正常)我收到错误,我的代码看起来像这样:
token = 'Token abc12345'
#tried the following:
#token = 'abc12345'
#token = 'Authorization: Token abc12345'
auth = coreapi.auth.TokenAuthentication(token=token)
client = coreapi.Client(auth=auth)
Run Code Online (Sandbox Code Playgroud)
试图访问架构,我得到:
coreapi.exceptions.ErrorMessage: <Error: 401 UNAUTHORIZED>
detail: "Authentication credentials were not provided."
Run Code Online (Sandbox Code Playgroud)
文档显示TokenAuthentication需要schema和token作为参数,但是该示例显示了使用JWT的TokenAuthentication,而不是djangos rest_framework.tokenauthentication.
任何意见,将不胜感激!
我想在使用我的核心 api 访问任何数据之前对用户进行授权,所以我尝试使用 JWT 身份验证。我在使用 api 登录用户时成功生成了令牌,并将该令牌保存在会话中的客户端,现在每当用户想要使用 api 访问任何数据时,我都会将该令牌在标头中发送到 api,并且我想验证该 JWT 令牌使用自定义授权过滤器。我已经创建了自定义授权过滤器并将其应用到我的 GetMenu api 上,并且我能够成功验证该令牌,但在授权过滤器中进行令牌验证后,它不会在我的 GetMenu api 上命中它。
这是我的 AccountController 代码:
[Filters.Authorization]
[AllowAnonymous]
[HttpPost]
[Route("GetMenu")]
public IActionResult GetMenu(string clientid, int rolecode, string repcode)
{
//further process
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Filters.Authorization 代码:
public class Authorization: AuthorizeAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
if (!ValidateToken(filterContext.HttpContext.Request.Headers["token"]))
{
filterContext.Result = new UnauthorizedResult();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在 OnAuthorization 方法和 GetMenu api 上有断点。我通过邮递员调用我的 GetMenu api 进行测试,它成功地在 Filters.Authorization 中的 OnAuthorization 方法上命中它并验证我的 JWT 令牌并在邮递员中显示状态代码:200,但在成功令牌验证后,它应该在 GetMenu api 上命中以进行进一步的操作正在处理数据,但没有命中。可能是什么问题?我缺少什么?请帮忙。
authorization core-api asp.net-authorization .net-core asp.net-core
是否可以在具有FileField的模型中使用HyperlinkedModelSerializer执行文件上传到DRF?
我正在使用utils包中的coreapi File类,而coreapi抱怨File对象不是JSON的主要(原文如此).
查看代码看起来模式必须说编码必须是多部分形式.
在哪里可以找到将这样的文件上传到DRF到具有FileField的模型的工作示例?
我正在尝试使用Enumerable#each_slice.它在我的计算机上不起作用,说明找不到该方法.
我正在运行ruby 1.8.6(2008-08-11 patchlevel 287)[universal-darwin9.0]
API:http://ruby-doc.org/core/classes/Enumerable.html#M003142
例:
(1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
core-api ×11
django ×4
python ×3
.net-core ×1
angular ×1
api ×1
asp.net-core ×1
docker ×1
enumerable ×1
file-upload ×1
mongoengine ×1
msdn ×1
psycopg2 ×1
python-3.x ×1
ruby ×1
swagger ×1
windows ×1