2016-07-16 03:13:57 +02:00
|
|
|
# Writing views in Zulip
|
|
|
|
|
|
|
|
## What this covers
|
|
|
|
|
|
|
|
This page documents how views work in Zulip. You may want to read the
|
2022-02-24 00:17:21 +01:00
|
|
|
[new feature tutorial](new-feature-tutorial.md)
|
2016-07-16 03:13:57 +02:00
|
|
|
and treat this as a reference.
|
|
|
|
|
|
|
|
If you have experience with Django, much of this will be familiar, but
|
|
|
|
you may want to read about how REST requests are dispatched, and how
|
|
|
|
request authentication works.
|
|
|
|
|
2022-02-24 00:17:21 +01:00
|
|
|
This document supplements the [new feature tutorial](new-feature-tutorial.md)
|
2019-09-30 19:37:56 +02:00
|
|
|
and the [testing](../testing/testing.md)
|
2016-07-16 03:13:57 +02:00
|
|
|
documentation.
|
|
|
|
|
|
|
|
## What is a view?
|
|
|
|
|
|
|
|
A view in Zulip is everything that helps implement a server endpoint.
|
|
|
|
Every path that the Zulip server supports (doesn't show a 404 page
|
|
|
|
for) is a view. The obvious ones are those you can visit in your
|
|
|
|
browser, for example
|
2020-06-08 23:04:39 +02:00
|
|
|
[/integrations](https://zulip.com/integrations/), which shows the
|
2016-07-16 03:13:57 +02:00
|
|
|
integration documentation. These paths show up in the address bar of
|
|
|
|
the browser. There are other views that are only seen by software,
|
|
|
|
namely the API views. They are used to build the various clients that
|
|
|
|
Zulip has, namely the web client (which is also used by the desktop
|
|
|
|
client) and the mobile clients.
|
|
|
|
|
|
|
|
## Modifying urls.py
|
|
|
|
|
|
|
|
A view is anything with an entry in the appropriate urls.py, usually
|
|
|
|
`zproject/urls.py`. Zulip views either serve HTML (pages for browsers)
|
|
|
|
or JSON (data for Zulip clients on all platforms, custom bots, and
|
|
|
|
integrations).
|
|
|
|
|
|
|
|
The format of the URL patterns in Django is [documented
|
2024-05-24 16:57:31 +02:00
|
|
|
here](https://docs.djangoproject.com/en/5.0/topics/http/urls/), and
|
2016-08-18 21:41:38 +02:00
|
|
|
the Zulip specific details for these are discussed in detail in the
|
2022-02-16 01:39:15 +01:00
|
|
|
[life of a request doc](life-of-a-request.md#options).
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
We have two Zulip-specific conventions we use for internationalization and for
|
|
|
|
our REST API, respectively.
|
|
|
|
|
|
|
|
## Writing human-readable views
|
|
|
|
|
|
|
|
If you're writing a new page for the website, make sure to add it
|
|
|
|
to `i18n_urls` in `zproject/urls.py`
|
|
|
|
|
|
|
|
```diff
|
|
|
|
i18n_urls = [
|
|
|
|
...
|
2020-06-24 13:12:38 +02:00
|
|
|
+ path('quote-of-the-day', TemplateView.as_view(template_name='zerver/qotd.html')),
|
|
|
|
+ path('postcards', 'zerver.views.postcards'),
|
2016-07-16 03:13:57 +02:00
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
As an example, if a request comes in for Spanish, language code `es`,
|
|
|
|
the server path will be something like: `es/features/`.
|
|
|
|
|
|
|
|
### Decorators used for webpage views
|
|
|
|
|
|
|
|
This section documents a few simple decorators that we use for webpage
|
|
|
|
views, as an introduction to view decorators.
|
|
|
|
|
|
|
|
`require_post`:
|
|
|
|
|
|
|
|
```py
|
|
|
|
|
|
|
|
@require_post
|
2020-05-09 00:10:17 +02:00
|
|
|
def accounts_register(request: HttpRequest) -> HttpResponse:
|
2016-07-16 03:13:57 +02:00
|
|
|
```
|
|
|
|
|
2018-11-12 01:56:56 +01:00
|
|
|
This decorator ensures that the request was a POST--here, we're
|
2016-07-16 03:13:57 +02:00
|
|
|
checking that the registration submission page is requested with a
|
|
|
|
post, and inside the function, we'll check the form data. If you
|
2024-05-20 22:16:21 +02:00
|
|
|
request this page with GET, you'll get an HTTP 405 METHOD NOT ALLOWED
|
2016-07-16 03:13:57 +02:00
|
|
|
error.
|
|
|
|
|
|
|
|
`zulip_login_required`:
|
|
|
|
|
|
|
|
This decorator verifies that the browser is logged in (i.e. has a
|
|
|
|
valid session cookie) before providing the view for this route, or
|
|
|
|
redirects the browser to a login page. This is used in the root path
|
|
|
|
(`/`) of the website for the web client. If a request comes from a
|
|
|
|
browser without a valid session cookie, they are redirected to a login
|
2021-08-20 21:53:28 +02:00
|
|
|
page. It is a small fork of Django's
|
2017-01-06 00:06:34 +01:00
|
|
|
[login_required][login-required-link], adding a few extra checks
|
|
|
|
specific to Zulip.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
```py
|
|
|
|
@zulip_login_required
|
2020-05-09 00:10:17 +02:00
|
|
|
def home(request: HttpRequest) -> HttpResponse:
|
2016-07-16 03:13:57 +02:00
|
|
|
```
|
|
|
|
|
2024-05-24 16:57:31 +02:00
|
|
|
[login-required-link]: https://docs.djangoproject.com/en/5.0/topics/auth/default/#django.contrib.auth.decorators.login_required
|
2017-01-06 00:06:34 +01:00
|
|
|
|
2016-07-16 03:13:57 +02:00
|
|
|
### Writing a template
|
|
|
|
|
|
|
|
Templates for the main website are found in
|
2021-09-01 00:15:31 +02:00
|
|
|
[templates/zerver/app](https://github.com/zulip/zulip/tree/main/templates/zerver/app).
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
## Writing API REST endpoints
|
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
These are code-parsable views that take x-www-form-urlencoded or JSON
|
2021-08-20 21:53:28 +02:00
|
|
|
request bodies, and return JSON-string responses. Almost all Zulip
|
2016-07-16 03:13:57 +02:00
|
|
|
view code is in the implementations of API REST endpoints.
|
|
|
|
|
|
|
|
The REST API does authentication of the user through `rest_dispatch`,
|
2017-01-06 00:06:34 +01:00
|
|
|
which is documented in detail at
|
2021-09-01 00:15:31 +02:00
|
|
|
[zerver/lib/rest.py](https://github.com/zulip/zulip/blob/main/zerver/lib/rest.py).
|
2016-07-16 03:13:57 +02:00
|
|
|
This method will authenticate the user either through a session token
|
|
|
|
from a cookie on the browser, or from a base64 encoded `email:api-key`
|
2020-10-23 02:43:28 +02:00
|
|
|
string given via HTTP basic auth for API clients.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```py
|
2016-07-16 03:13:57 +02:00
|
|
|
>>> import requests
|
|
|
|
>>> r = requests.get('https://api.github.com/user', auth=('hello@example.com', '0123456789abcdeFGHIJKLmnopQRSTUV'))
|
|
|
|
>>> r.status_code
|
|
|
|
-> 200
|
|
|
|
```
|
|
|
|
|
|
|
|
### Request variables
|
|
|
|
|
|
|
|
Most API views will have some arguments that are passed as part of the
|
2021-08-20 21:53:28 +02:00
|
|
|
request to control the behavior of the view. In any well-engineered
|
2016-07-16 03:13:57 +02:00
|
|
|
view, you need to write code to parse and validate that the arguments
|
2021-08-20 21:53:28 +02:00
|
|
|
exist and have the correct form. For many applications, this leads to
|
2016-10-15 12:04:40 +02:00
|
|
|
one of several bad outcomes:
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The code isn't written, so arguments aren't validated, leading to
|
2016-07-16 03:13:57 +02:00
|
|
|
bugs and confusing error messages for users of the API.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Every function starts with a long list of semi-redundant validation
|
2016-07-16 03:13:57 +02:00
|
|
|
code, usually with highly inconsistent error messages.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Every view function comes with another function that does the
|
2016-07-16 03:13:57 +02:00
|
|
|
validation that has the problems from the last bullet point.
|
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
In Zulip, we solve this problem with a special decorator called
|
2024-08-02 19:38:27 +02:00
|
|
|
`typed_endpoint` which allows a developer to declare the
|
2016-07-16 03:13:57 +02:00
|
|
|
arguments a view function takes and validate their types all within
|
2024-08-02 19:38:27 +02:00
|
|
|
the `def` line of the function. This framework uses
|
|
|
|
[Pydantic V2](https://docs.pydantic.dev/dev/) to perform data validation
|
|
|
|
and parsing for the view arguments. We like this framework because we
|
2016-07-16 03:13:57 +02:00
|
|
|
have found it makes the validation code compact, readable, and
|
|
|
|
conveniently located in the same place as the method it is validating
|
|
|
|
arguments for.
|
|
|
|
|
|
|
|
Here's an example:
|
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```py
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.decorator import require_realm_admin
|
2024-08-02 19:38:27 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
@require_realm_admin
|
2024-08-02 19:38:27 +02:00
|
|
|
@typed_endpoint
|
|
|
|
def create_user_backend(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
*,
|
|
|
|
email: str,
|
|
|
|
password: str,
|
|
|
|
full_name: str,
|
|
|
|
):
|
2016-07-16 03:13:57 +02:00
|
|
|
# ... code here
|
|
|
|
```
|
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
The `typed_endpoint` decorator parses the declared
|
|
|
|
[keyword-only arguments](https://docs.python.org/3/glossary.html#term-parameter)
|
|
|
|
of the decorated function, and for each argument that has been declared,
|
|
|
|
it extracts the HTTP parameter with that name from the request,
|
|
|
|
parses it according to the type annotation, and then passes it to
|
2021-08-20 21:53:28 +02:00
|
|
|
the function. It will return an nicely JSON formatted HTTP 400 error
|
2016-07-16 03:13:57 +02:00
|
|
|
in the event that an argument is missing, doesn't parse as JSON, or
|
|
|
|
otherwise is invalid.
|
|
|
|
|
|
|
|
`require_realm_admin` is another decorator which checks the
|
|
|
|
authorization of the given `user_profile` to make sure it belongs to a
|
|
|
|
realm administrator (and thus has permission to create a user); we
|
2024-08-02 19:38:27 +02:00
|
|
|
show it here primarily to show how `typed_endpoint` should be
|
2016-07-16 03:13:57 +02:00
|
|
|
the inner decorator.
|
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
The implementation of `typed_endpoint` is documented in detail
|
2016-07-16 03:13:57 +02:00
|
|
|
in
|
2024-08-02 19:38:27 +02:00
|
|
|
[zerver/lib/typed_endpoint.py](https://github.com/zulip/zulip/blob/main/zerver/lib/typed_endpoint.py)
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
Pydantic also helps us with request variable validation. For example:
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
- `msg_ids: Json[list[int]]` will check that the `msg_ids`
|
|
|
|
HTTP parameter is a list of integers, marshalled as JSON,
|
|
|
|
and pass it into the function as the `msg_ids` Python
|
2021-04-07 22:00:44 +02:00
|
|
|
keyword argument.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
- `streams_raw: Annotated[Json[list[str]], ApiParamConfig("subscriptions")]`
|
2021-09-08 00:23:24 +02:00
|
|
|
will check that the "subscriptions" HTTP parameter is a list of
|
|
|
|
strings, marshalled as JSON, and pass it into the function with the
|
|
|
|
Python keyword argument `streams_raw`.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
- `message_id: Json[NonNegativeInt]` will check that the `message_id`
|
|
|
|
HTTP parameter is a string containing a JSON encoded non-negative
|
|
|
|
integer.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
[Annotated](https://docs.python.org/3/library/typing.html#typing.Annotated)
|
|
|
|
can be used in combination with
|
|
|
|
[Pydantic's validators](https://docs.pydantic.dev/latest/api/functional_validators/)
|
|
|
|
to provide additional validation for the arguments.
|
|
|
|
|
|
|
|
- `name: Annotated[str, StringConstraints(max_length=60)]` will check that the
|
|
|
|
`name` HTTP parameter is a string containing up to 60 characters.
|
|
|
|
|
|
|
|
- Since there is no need to JSON-encode strings
|
|
|
|
(lists, integers, bools and complex objects require JSON encoding), usually simply
|
|
|
|
`my_string: str` is correct. One can pass, for example,
|
|
|
|
`Annotated[str, check_string_in_validator(...)]` where one wants to run a
|
2021-05-08 00:12:18 +02:00
|
|
|
validator on the value of a string.
|
|
|
|
|
2024-08-02 19:38:27 +02:00
|
|
|
Default values can be specified for optional arguments similar to how we would specify
|
|
|
|
default values in regular python function.
|
|
|
|
|
|
|
|
- `is_default_stream: Json[bool] = False` will assign False to the `is_default_stream` argument
|
|
|
|
if no value is specified when making a request to the endpoint.
|
|
|
|
|
|
|
|
- We can use `None` as the default value for optional arguments when we don't
|
|
|
|
want to specify any specific default value, for example,
|
|
|
|
`narrow: Json[list[NarrowParameter]] | None = None`. This does not allow the
|
|
|
|
caller to pass `None` as the value, the only way `narrow` can be set to `None` is
|
|
|
|
by using the default value.
|
|
|
|
|
|
|
|
[Pydantic models](https://docs.pydantic.dev/latest/concepts/models/) can be used to
|
|
|
|
define the schema of complex objects that can be passed to the endpoint.
|
|
|
|
|
|
|
|
Here's an example:
|
|
|
|
|
|
|
|
```py
|
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
from pydantic import BaseModel, StringConstraints, model_validator
|
|
|
|
|
|
|
|
class AddSubscriptionData(BaseModel):
|
|
|
|
name: str
|
|
|
|
color: str | None = None
|
|
|
|
description: (
|
|
|
|
Annotated[str, StringConstraints(max_length=Stream.MAX_DESCRIPTION_LENGTH)] | None
|
|
|
|
) = None
|
|
|
|
|
|
|
|
@model_validator(mode="after")
|
|
|
|
def validate_terms(self) -> "AddSubscriptionData":
|
|
|
|
# ... Validation logic here
|
|
|
|
return self
|
|
|
|
```
|
|
|
|
|
|
|
|
- `add: Json[list[AddSubscriptionData]]` will require the `add` argument to be a list of objects
|
|
|
|
having the keys that are specified in the `AddSubscriptionData` model.
|
|
|
|
|
|
|
|
- `@model_validator` can be used to specify additional validation logic for the model.
|
|
|
|
|
2017-01-05 23:23:16 +01:00
|
|
|
See
|
2024-08-02 19:38:27 +02:00
|
|
|
[zerver/lib/typed_endpoint_validators.py](https://github.com/zulip/zulip/blob/main/zerver/lib/typed_endpoint_validators.py)
|
2017-01-05 23:23:16 +01:00
|
|
|
for more validators and their documentation.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
### Deciding which HTTP verb to use
|
|
|
|
|
|
|
|
When writing a new API view, you should writing a view to do just one
|
2021-08-20 21:53:28 +02:00
|
|
|
type of thing. Usually that's either a read or write operation.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
If you're reading data, GET is the best option. Other read-only verbs
|
|
|
|
are HEAD, which should be used for testing if a resource is available to
|
|
|
|
be read with GET, without the expense of the full GET. OPTIONS is also
|
|
|
|
read-only, and used by clients to determine which HTTP verbs are
|
|
|
|
available for a given path. This isn't something you need to write, as
|
|
|
|
it happens automatically in the implementation of `rest_dispatch`--see
|
2021-09-01 00:15:31 +02:00
|
|
|
[zerver/lib/rest.py](https://github.com/zulip/zulip/blob/main/zerver/lib/rest.py)
|
2016-07-16 03:13:57 +02:00
|
|
|
for more.
|
|
|
|
|
|
|
|
If you're creating new data, try to figure out if the thing you are
|
|
|
|
creating is uniquely identifiable. For example, if you're creating a
|
|
|
|
user, there's only one user per email. If you can find a unique ID,
|
|
|
|
you should use PUT for the view. If you want to create the data multiple
|
|
|
|
times for multiple requests (for example, requesting the send_message
|
|
|
|
view multiple times with the same content should send multiple
|
|
|
|
messages), you should use POST.
|
|
|
|
|
|
|
|
If you're updating existing data, use PATCH.
|
|
|
|
|
|
|
|
If you're removing data, use DELETE.
|
|
|
|
|
|
|
|
### Idempotency
|
|
|
|
|
|
|
|
When writing a new API endpoint, with the exception of things like
|
|
|
|
sending messages, requests should be safe to repeat, without impacting
|
2021-08-20 22:54:08 +02:00
|
|
|
the state of the server. This is _idempotency_.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
You will often want to return an error if a request to change
|
|
|
|
something would do nothing because the state is already as desired, to
|
|
|
|
make debugging Zulip clients easier. This means that the response for
|
|
|
|
repeated requests may not be the same, but the repeated requests won't
|
|
|
|
change the server more than once or cause unwanted side effects.
|
|
|
|
|
|
|
|
### Making changes to the database
|
|
|
|
|
|
|
|
If the view does any modification to the database, that change is done
|
2022-04-14 00:48:36 +02:00
|
|
|
in a helper function in `zerver/actions/*.py`. Those functions are
|
2016-07-16 03:13:57 +02:00
|
|
|
responsible for doing a complete update to the state of the server,
|
|
|
|
which often entails both updating the database and sending any events
|
2021-08-20 21:53:28 +02:00
|
|
|
to notify clients about the state change. When possible, we prefer to
|
2016-07-16 03:13:57 +02:00
|
|
|
design a clean boundary between the view function and the actions
|
|
|
|
function is such that all user input validation happens in the view
|
|
|
|
code (i.e. all 400 type errors are thrown there), and the actions code
|
|
|
|
is responsible for atomically executing the change (this is usually
|
|
|
|
signalled by having the actions function have a name starting with
|
2024-07-30 11:39:32 +02:00
|
|
|
`do_`). So in most cases, errors in an actions function will be the
|
2024-07-04 12:33:43 +02:00
|
|
|
result of an operational problem (e.g., lost connection to the
|
2021-08-20 21:53:28 +02:00
|
|
|
database) and lead to a 500 error. If an actions function is
|
2016-07-16 03:13:57 +02:00
|
|
|
responsible for validation as well, it should have a name starting
|
|
|
|
with `check_`.
|
|
|
|
|
2021-09-01 00:15:31 +02:00
|
|
|
For example, in [zerver/views/realm.py](https://github.com/zulip/zulip/blob/main/zerver/views/realm.py):
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
```py
|
|
|
|
@require_realm_admin
|
2024-08-02 19:38:27 +02:00
|
|
|
@typed_endpoint
|
2020-05-09 00:10:17 +02:00
|
|
|
def update_realm(
|
2024-08-02 19:38:27 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
*,
|
|
|
|
name: Annotated[str | None, StringConstraints(max_length=Realm.MAX_REALM_NAME_LENGTH)] = None,
|
2020-05-09 00:10:17 +02:00
|
|
|
# ...
|
|
|
|
):
|
2016-07-16 03:13:57 +02:00
|
|
|
realm = user_profile.realm
|
2020-05-09 00:10:17 +02:00
|
|
|
# ...
|
2020-06-29 15:34:19 +02:00
|
|
|
do_set_realm_property(realm, k, v, acting_user=user_profile)
|
2020-05-09 00:10:17 +02:00
|
|
|
# ...
|
2016-07-16 03:13:57 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
`realm.save()` actually saves the changes to the realm to the
|
2024-09-21 00:28:18 +02:00
|
|
|
database, and `send_event_on_commit` sends the event to active clients
|
|
|
|
belonging to the provided list of users (in this case, all active
|
|
|
|
users in the Zulip realm), once the current transaction completes.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
### Calling from the web application
|
|
|
|
|
2019-04-23 00:40:19 +02:00
|
|
|
You should always use `channel.<method>` to make an `HTTP <method>` call
|
2016-07-16 03:13:57 +02:00
|
|
|
to the Zulip JSON API. As an example, in
|
2023-02-22 23:03:47 +01:00
|
|
|
[web/src/admin.js](https://github.com/zulip/zulip/blob/main/web/src/admin.js)
|
2016-07-16 03:13:57 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
var url = "/json/realm";
|
|
|
|
var data = {
|
|
|
|
name: JSON.stringify(new_name),
|
|
|
|
}
|
|
|
|
channel.patch({
|
|
|
|
url: url,
|
|
|
|
data: data,
|
|
|
|
success: function (response_data) {
|
|
|
|
if (response_data.name !== undefined) {
|
2021-04-10 09:38:17 +02:00
|
|
|
ui_report.success($t({defaultMessage: "Name changed!"}), name_status);
|
2016-07-16 03:13:57 +02:00
|
|
|
}
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
### Calling from an API client
|
|
|
|
|
|
|
|
Here's how you might manually make a call from python:
|
|
|
|
|
|
|
|
```py
|
|
|
|
payload = {'name': new_name}
|
|
|
|
|
|
|
|
# email and API key
|
|
|
|
api_auth = ('hello@example.com', '0123456789abcdeFGHIJKLmnopQRSTUV')
|
|
|
|
|
|
|
|
r = requests.patch(SERVER_URL + 'api/v1/realm',
|
|
|
|
data=json.dumps(payload),
|
|
|
|
auth=api_auth,
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
This is simply an illustration; we recommend making use of the [Zulip
|
2024-02-15 02:42:15 +01:00
|
|
|
Python API bindings](https://zulip.com/api/) since they provide
|
2016-07-16 03:13:57 +02:00
|
|
|
a nice interface for accessing the API.
|
|
|
|
|
|
|
|
## Legacy endpoints used by the web client
|
|
|
|
|
|
|
|
New features should conform the REST API style. The legacy, web-only
|
|
|
|
endpoints can't effectively enforce usage of a browser, so they aren't
|
|
|
|
preferable from a security perspective, and it is generally a good idea
|
|
|
|
to make your feature available to other clients, especially the mobile
|
|
|
|
clients.
|
|
|
|
|
2020-06-24 03:22:41 +02:00
|
|
|
These endpoints make use the older authentication decorator
|
|
|
|
`authenticated_json_view`, so you may see it in the code.
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2018-05-31 01:56:18 +02:00
|
|
|
## Incoming webhook integrations
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2018-05-31 01:56:18 +02:00
|
|
|
Incoming webhooks are called by other services, often to send a message as part
|
2016-07-16 03:13:57 +02:00
|
|
|
of those services' integrations. They are most often POST requests, and
|
|
|
|
often there is very little you can customize about them. Usually you can
|
|
|
|
expect that the webhook for a service will allow specification for the
|
|
|
|
target server for the webhook, and an API key.
|
|
|
|
|
|
|
|
If the webhook does not have an option to provide a bot email, use the
|
2020-08-20 00:32:15 +02:00
|
|
|
`webhook_view` decorator, to fill in the `user_profile` and
|
2017-05-02 01:00:50 +02:00
|
|
|
`request.client` fields of a request:
|
2016-07-16 03:13:57 +02:00
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```py
|
2024-08-02 19:38:27 +02:00
|
|
|
@webhook_view("PagerDuty", all_event_types=ALL_EVENT_TYPES)
|
|
|
|
@typed_endpoint
|
|
|
|
def api_pagerduty_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
*,
|
|
|
|
payload: JsonBodyPayload[WildValue],
|
2016-07-16 03:13:57 +02:00
|
|
|
```
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2017-05-02 01:00:50 +02:00
|
|
|
`request.client` will be the result of `get_client("ZulipPagerDutyWebhook")`
|
2017-10-02 07:02:11 +02:00
|
|
|
in this example and it will be passed to `check_send_stream_message`. For
|
2019-09-30 19:37:56 +02:00
|
|
|
more information, see [Clients in Zulip](../subsystems/client.md).
|