mt5api 2.0.0__tar.gz → 2.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mt5api
3
- Version: 2.0.0
3
+ Version: 2.0.1
4
4
  Summary: MetaTrader 5 REST API
5
5
  Project-URL: Repository, https://github.com/dceoy/mt5api.git
6
6
  Author-email: dceoy <dceoy@users.noreply.github.com>
@@ -6,16 +6,17 @@ import logging
6
6
  import time
7
7
  from typing import TYPE_CHECKING, Any, cast
8
8
 
9
- from fastapi import HTTPException, Request, status
9
+ from fastapi import Request, status
10
10
  from fastapi.exceptions import RequestValidationError
11
11
  from fastapi.responses import JSONResponse
12
12
  from pdmt5.mt5 import Mt5RuntimeError
13
13
  from pydantic import ValidationError
14
+ from starlette.exceptions import HTTPException
14
15
 
15
16
  from .models import ErrorResponse
16
17
 
17
18
  if TYPE_CHECKING:
18
- from collections.abc import Sequence
19
+ from collections.abc import Mapping, Sequence
19
20
 
20
21
  from fastapi import FastAPI
21
22
  from starlette.middleware.base import RequestResponseEndpoint
@@ -60,6 +61,7 @@ def _create_error_response(
60
61
  status_code: int,
61
62
  detail: str,
62
63
  instance: str,
64
+ headers: Mapping[str, str] | None = None,
63
65
  ) -> JSONResponse:
64
66
  """Create a RFC 7807 Problem Details error response.
65
67
 
@@ -69,6 +71,7 @@ def _create_error_response(
69
71
  status_code: HTTP status code.
70
72
  detail: Detailed error explanation.
71
73
  instance: Request URI that caused the error.
74
+ headers: Optional response headers (e.g. ``Allow`` for 405).
72
75
 
73
76
  Returns:
74
77
  JSONResponse with error details.
@@ -80,7 +83,11 @@ def _create_error_response(
80
83
  detail=detail,
81
84
  instance=instance,
82
85
  )
83
- return JSONResponse(status_code=status_code, content=error.model_dump())
86
+ return JSONResponse(
87
+ status_code=status_code,
88
+ content=error.model_dump(),
89
+ headers=headers,
90
+ )
84
91
 
85
92
 
86
93
  async def error_handler_middleware(
@@ -199,7 +206,10 @@ def http_exception_handler(
199
206
  request: Request,
200
207
  exc: HTTPException,
201
208
  ) -> JSONResponse:
202
- """Convert FastAPI HTTP exceptions to flat RFC 7807 responses.
209
+ """Convert HTTP exceptions to flat RFC 7807 responses.
210
+
211
+ Registered for Starlette's base class so router-level errors (404, 405)
212
+ are covered as well as FastAPI ``HTTPException`` subclasses.
203
213
 
204
214
  Returns:
205
215
  Problem Details JSON response.
@@ -220,6 +230,7 @@ def http_exception_handler(
220
230
  exc.status_code,
221
231
  detail,
222
232
  str(request.url),
233
+ headers=exc.headers,
223
234
  )
224
235
 
225
236
 
@@ -491,6 +491,22 @@ class OrdersRequest(BaseModel):
491
491
  format: ResponseFormat | None = Field(default=None)
492
492
 
493
493
 
494
+ def _validate_date_range(date_from: datetime, date_to: datetime) -> None:
495
+ """Ensure ``date_from`` precedes ``date_to``.
496
+
497
+ Raises:
498
+ ValueError: If the range is empty or mixes naive and aware datetimes.
499
+ """
500
+ try:
501
+ is_empty = date_from >= date_to
502
+ except TypeError as error:
503
+ message = "date_from and date_to must both include or both omit a UTC offset"
504
+ raise ValueError(message) from error
505
+ if is_empty:
506
+ range_error = "date_from must be before date_to"
507
+ raise ValueError(range_error)
508
+
509
+
494
510
  class HistoryRequestBase(BaseModel):
495
511
  """Shared fields and validation for history endpoints."""
496
512
 
@@ -525,13 +541,8 @@ class HistoryRequestBase(BaseModel):
525
541
  )
526
542
  raise ValueError(error_message)
527
543
 
528
- if (
529
- self.date_from is not None
530
- and self.date_to is not None
531
- and self.date_from >= self.date_to
532
- ):
533
- range_error = "date_from must be before date_to"
534
- raise ValueError(range_error)
544
+ if self.date_from is not None and self.date_to is not None:
545
+ _validate_date_range(self.date_from, self.date_to)
535
546
 
536
547
  return self
537
548
 
@@ -572,13 +583,8 @@ class HistoryTotalRequest(BaseModel):
572
583
 
573
584
  Returns:
574
585
  The validated model instance.
575
-
576
- Raises:
577
- ValueError: If date_from is not before date_to.
578
586
  """
579
- if self.date_from >= self.date_to:
580
- range_error = "date_from must be before date_to"
581
- raise ValueError(range_error)
587
+ _validate_date_range(self.date_from, self.date_to)
582
588
  return self
583
589
 
584
590
 
@@ -52,10 +52,11 @@ async def post_connection_login(
52
52
  async with state.client_lock:
53
53
  await replace_mt5_client(state, config)
54
54
  await release_market_book_subscriptions(state)
55
+ sanitized_server = request.server.replace("\r", "").replace("\n", "")
55
56
  logger.info(
56
57
  "MT5 client reconnected (login=%d, server=%s)",
57
58
  request.login,
58
- request.server,
59
+ sanitized_server,
59
60
  )
60
61
  return LoginResponse(
61
62
  login=request.login,
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mt5api"
3
- version = "2.0.0"
3
+ version = "2.0.1"
4
4
  description = "MetaTrader 5 REST API"
5
5
  authors = [{name = "dceoy", email = "dceoy@users.noreply.github.com"}]
6
6
  maintainers = [{name = "dceoy", email = "dceoy@users.noreply.github.com"}]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes