Skip to content

Commit 89d8060

Browse files
committed
update for contacts and webhooks
1 parent 4690e9f commit 89d8060

File tree

18 files changed

+541
-99
lines changed

18 files changed

+541
-99
lines changed

CHANGELOG.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/).
77

8-
## [2.3.6] - 2024-05-10
8+
## [2.13.0] - 2024-08-03
99

1010
### Added
1111

12-
- When `environments` property is set on the `context` the global `environment` value is overwriten.
12+
- Added support for webhooks signature verification
13+
- Added support for the `contacts` API
14+
15+
## [2.12.0] - 2024-07-09
16+
17+
### Added
18+
19+
- Added support for `prefix_messages` in deployments
1320

1421
## [2.11.0] - 2024-06-30
1522

@@ -22,8 +29,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
2229

2330
- Centralize the `api_key` in the `Store` object to reuse it in all the API calls
2431

25-
## [2.12.0] - 2024-07-09
32+
33+
34+
35+
## [2.3.6] - 2024-05-10
2636

2737
### Added
2838

29-
- Added support for `prefix_messages` in deployments
39+
- When `environments` property is set on the `context` the global `environment` value is overwriten.
40+

orq_ai_sdk/api_resources/async_deployments.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
from orq_ai_sdk.http_client import post_async, stream_async
99

10-
DEPLOYMENTS_API = "{}/deployments".format(BASE_URL)
10+
DEPLOYMENTS_API = "{}/v2/deployments".format(BASE_URL)
1111

12-
GET_CONFIG_URL = "{}/get_config".format(DEPLOYMENTS_API)
13-
INVOKE_URL = "{}/invoke".format(DEPLOYMENTS_API)
12+
GET_CONFIG_URL = "{}/v2/get_config".format(DEPLOYMENTS_API)
13+
INVOKE_URL = "{}/v2/invoke".format(DEPLOYMENTS_API)
1414

1515
from typing import Optional, TypedDict
1616

@@ -306,11 +306,6 @@ def __validate_params(
306306
if extra_params is not None:
307307
self.body_params["extra_params"] = extra_params
308308

309-
user_info = Store.get("user_info")
310-
311-
if user_info is not None and isinstance(user_info, dict):
312-
self.body_params["user_id"] = user_info.get("id")
313-
314309
async def get_config(self, key: str, context=None, inputs=None, metadata=None):
315310
self.__validate_params(
316311
key=key, context=context, inputs=inputs, metadata=metadata
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Contacts
2+
3+
The Contacts API allows you to create and manage contact information in the orq.ai API. This is useful for associating
4+
user data with AI interactions.
5+
6+
## Set the contact using the client
7+
8+
To set a contact ID in the store and create a contact simultaneously, use the exposed `set_contact` method from the
9+
client:
10+
11+
```python:
12+
client.set_contact(
13+
id="user123",
14+
display_name="John Doe",
15+
email="john@example.com",
16+
avatar_url="https://example.com/avatar.jpg",
17+
metadata={"role": "admin"}
18+
)
19+
```
20+
21+
This method combines setting the contact ID in the store and creating a contact in one operation.
22+
23+
## Creating a Contact
24+
25+
To create a contact, use the `create` method of the `client.contacts`:
26+
27+
```python
28+
contact = client.contacts.create(
29+
external_id="user123",
30+
display_name="John Doe",
31+
email="john@example.com",
32+
avatar_url="https://example.com/avatar.jpg",
33+
metadata={"role": "admin"}
34+
)
35+
```
36+
37+
## Asynchronous Creation
38+
39+
For asynchronous operations, use the `acreate` method:
40+
41+
```python
42+
contact = await client.contacts.acreate(
43+
external_id="user123",
44+
display_name="John Doe",
45+
email="john@example.com",
46+
avatar_url="https://example.com/avatar.jpg",
47+
metadata={"role": "admin"}
48+
)
49+
```
50+
51+
## Best Practices
52+
53+
1. Always provide a unique `external_id` for each contact. This helps in identifying and managing contacts in your
54+
system.
55+
56+
2. Handle errors appropriately when creating contacts, as network issues or validation errors may occur.
57+
58+
3. Use the asynchronous `acreate` method for better performance in asynchronous applications.
59+
60+
4. Ensure that the `email` and `avatar_url` fields, if provided, are in valid formats to avoid validation errors.
61+
62+
5. Use the `metadata` field to store additional information about the contact that doesn't fit into the standard fields.
63+
64+
## API Reference
65+
66+
### `create(external_id: str, display_name: str | None = None, email: str | None = None, avatar_url: str | None = None, metadata: dict | None = None) -> Contact`
67+
68+
Creates a new contact in the orq.ai API.
69+
70+
- `external_id`: str - Required. An external identifier for the user.
71+
- `display_name`: str | None - Optional. The user's display name or full name.
72+
- `email`: str | None - Optional. The user's email address.
73+
- `avatar_url`: str | None - Optional. URL to the user's avatar image.
74+
- `metadata`: dict | None - Optional. Additional key-value pairs for storing extra user information.
75+
76+
Returns a `Contact` object.
77+
78+
### `acreate(external_id: str, display_name: str | None = None, email: str | None = None, avatar_url: str | None = None, metadata: dict | None = None) -> Contact`
79+
80+
Asynchronously creates a new contact in the orq.ai API. Parameters are the same as `create`.
81+
82+
Returns a `Contact` object.
83+
84+
## Error Handling
85+
86+
- If `external_id` is not provided, a `ValueError` will be raised.
87+
- If the API request fails, an HTTP error will be raised. You should handle these exceptions in your code.
88+
89+
## Example
90+
91+
```python
92+
try:
93+
contact = client.contacts.create(
94+
external_id="user123",
95+
display_name="John Doe",
96+
email="john@example.com",
97+
avatar_url="https://example.com/avatar.jpg",
98+
metadata={"role": "admin", "department": "IT"}
99+
)
100+
print(f"Contact created: {contact.id}")
101+
except ValueError as e:
102+
print(f"Validation error: {e}")
103+
except Exception as e:
104+
print(f"An error occurred: {e}")
105+
```

orq_ai_sdk/api_resources/contacts/__init__.py

Whitespace-only changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from typing import Dict, Any, Union
2+
3+
from pydantic import BaseModel
4+
5+
from orq_ai_sdk.constants import BASE_URL
6+
from orq_ai_sdk.http_client import post, post_async
7+
8+
CONTACTS_API = '{}{}'.format(BASE_URL, '/v2/contacts')
9+
10+
11+
class Contact(BaseModel):
12+
id: str
13+
external_id: str
14+
display_name: Union[str, None] = None
15+
email: Union[str, None] = None
16+
avatar_url: Union[str, None] = None
17+
metadata: Dict[str, Any] = dict
18+
19+
20+
class Contacts:
21+
def create(
22+
self,
23+
external_id: str,
24+
display_name: None,
25+
email: None,
26+
avatar_url: None,
27+
metadata=None
28+
):
29+
"""
30+
Attributes:
31+
:param external_id: An external identifier for the user
32+
:param display_name: The user's display name or full name
33+
:param email: The user's email address (validated format)
34+
:param avatar_url: URL to the user's avatar image (validated URL format)
35+
:param metadata: Additional key-value pairs for storing extra contact information
36+
:return:
37+
"""
38+
39+
if not external_id:
40+
raise ValueError("External ID is required.")
41+
42+
response = post(
43+
url=CONTACTS_API,
44+
body={
45+
"external_id": external_id,
46+
"display_name": display_name,
47+
"email": email,
48+
"avatar_url": avatar_url,
49+
"metadata": metadata
50+
}
51+
)
52+
53+
response.raise_for_status()
54+
55+
return Contact(**response.json())
56+
57+
async def acreate(
58+
self,
59+
external_id: str,
60+
display_name: None,
61+
email: None,
62+
avatar_url: None,
63+
metadata=dict
64+
):
65+
"""
66+
Attributes:
67+
:param external_id: An external identifier for the user
68+
:param display_name: The user's display name or full name
69+
:param email: The user's email address (validated format)
70+
:param avatar_url: URL to the user's avatar image (validated URL format)
71+
:param metadata: Additional key-value pairs for storing extra contact information
72+
:return:
73+
"""
74+
75+
if not external_id:
76+
raise ValueError("External ID is required.")
77+
78+
response = await post_async(
79+
url=CONTACTS_API,
80+
body={
81+
"external_id": external_id,
82+
"display_name": display_name,
83+
"email": email,
84+
"avatar_url": avatar_url,
85+
"metadata": metadata
86+
}
87+
)
88+
89+
response.raise_for_status()
90+
91+
return Contact(**response.json())

0 commit comments

Comments
 (0)