Mock Process Drawings¶
When using Werk24 for developing your automations, you may need to frequently test the same engineering drawings. Recognizing this, Werk24 offers a Mock Interface, allowing you to simulate the processing of files without incurring additional charges. This feature is especially useful during the initial phases of development, ensuring you pay only once for processing the same file. Another benefit is the considerable time savings, as mock processing can deliver results in just a few seconds. To utilize this functionality, simply follow the guidelines outlined in our tutorial.
What will I learn?¶
- Mock Processing technical drawings.
- Avoiding deduction of files from test account or development account, while processing files for developemnt purpose.
- Saving significant processing time
Step 1: Import all the requirements¶
Let's get imports out of the way at the very beginning.
from werk24.techread_client import (
W24TechreadClient,
W24Ask,
W24TechreadInitResponse,
Hook,
)
from werk24.models.techread import (
W24TechreadMessage,
W24TechreadMessageType,
W24TechreadMessageSubtypeAsk,
W24TechreadMessageSubtypeProgress,
W24PresignedPost,
)
from werk24.models.ask import W24AskTitleBlock
from werk24.utils import w24_read_sync
from typing import List, Optional, AsyncGenerator, Tuple
import uuid
from pydantic import UUID4
# define a function to read a file and return it as bytes
from . import get_drawing_bytes
request_id: Optional[UUID4] = None
Step 2: Mock the initialization¶
The initialization tells the server that you want to upload files. The server then generates two URLS for you that allow you to upload the drawing and the model.
async def mock_init_request(
self,
asks: List[W24Ask],
max_pages: int,
drawing_filename: str,
sub_account: Optional[UUID4] = None,
) -> Tuple[W24TechreadMessage, W24TechreadInitResponse]:
"""Mock the initialization.
The initialization tells the server that you want to
upload files. The server then generates two URLS for
you that allow you to upload the drawing and the model.
Args:
----
asks (List[W24Ask]): List of all your asks.
max_pages (int): Max number of pages shall be processed.
drawing_filename (str): Filename of the drawing.
sub_account (Optional[UUID4]): Optional sub_account.
Returns:
-------
Tuple[W24TechreadMessage, W24TechreadInitResponse]: Returns the
raw mesage as well as the parsed InitResponse
"""
global request_id
request_id = uuid.uuid4()
message = W24TechreadMessage(
request_id=request_id,
message_type=W24TechreadMessageType.PROGRESS,
message_subtype=W24TechreadMessageSubtypeProgress.INITIALIZATION_SUCCESS,
page_number=0,
payload_dict={
"drawing_presigned_post": W24PresignedPost(
url="https://werk24.io"
).model_dump(),
"model_presigned_post": W24PresignedPost(
url="https://werk24.io"
).model_dump(),
},
)
response = W24TechreadInitResponse.model_validate(message.payload_dict)
return message, response
Step 3: Mock the server response¶
Next, we need to mock the response that you get from Werk24's API. This would be starting of “processing of the file”, followed by the extracted information and the message “end of processing”.
async def mock_read_request(
self,
init_response: W24TechreadInitResponse,
asks: List[W24Ask],
drawing: bytes,
model: Optional[bytes],
) -> AsyncGenerator[W24TechreadMessage, None]:
"""Mock the server response.
Args:
----
init_response (W24TechreadInitResponse): Initialization response
asks (List[W24Ask]): List of your asks
drawing (bytes): Content of the drawing
model (Optional[bytes]): Content of the model
Returns:
-------
AsyncGenerator[W24TechreadMessage, None]: Generator of response
messages.
"""
global request_id
# Yield the message: Server started processing the file
yield W24TechreadMessage(
request_id=request_id,
message_type=W24TechreadMessageType.PROGRESS,
message_subtype=W24TechreadMessageSubtypeProgress.STARTED,
)
# Yield the message: Here is your data
yield W24TechreadMessage(
request_id=request_id,
message_type=W24TechreadMessageType.ASK,
message_subtype=W24TechreadMessageSubtypeAsk.TITLE_BLOCK,
page_number=0,
payload_dict={"a": "b"},
)
# Yield the message: Server is done processing your fle
yield W24TechreadMessage(
request_id=request_id,
message_type=W24TechreadMessageType.PROGRESS,
message_subtype=W24TechreadMessageSubtypeProgress.COMPLETED,
)
Step 4: Overwrite the methods¶
Now, we need to overwrite the existing methods defined in Werk24's API by default.
W24TechreadClient.init_request = mock_init_request
W24TechreadClient.read_request = mock_read_request
Step 5: Define your normal Callback handler¶
Now, we define how you need the response from Werk24.
def callback(message: W24TechreadMessage):
print("called")
print(message)
Step 6: Mock process the files¶
Voila! With the Mock Interface feature of Werk24, you are now able to process files in a way that mirrors actual API interactions, while effectively preserving your file quota. This approach ensures that you receive the same API responses as with regular processing, but without the associated file usage counts.
ask = W24AskTitleBlock()
w24_read_sync(
get_drawing_bytes(),
hooks=[Hook(ask=ask, function=callback)],
max_pages=1,
)