Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
prtolem committed Mar 29, 2024
1 parent 961618a commit 632c507
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,35 @@ Asynchronous API wrapper for https://docs.mail.tm/
* [Set read message by id](#Set-read-message-by-id)
* [Get message source by id](#Get-message-source-by-id)

## Setup
```shell
pip install MailTMAPI
```

## Usage example
```python
import asyncio

from mailtm import MailTM


def main() -> None:
async def main() -> None:
mailtm = MailTM()
result = await mailtm.get_account_token(address="example", password="example")
print(result)
temp_mail = await mailtm.get_account()
print(temp_mail.address)


if __name__ == '__main__':
main()
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
```

## API methods
A list of all available methods with their parameters and response.

### Authentication
```python
get_account_token(address, password)
await get_account_token(address, password)
```

| Parameter | Type | Description |
Expand Down
15 changes: 11 additions & 4 deletions mailtm/mailtm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging

from aiohttp import ClientSession
Expand All @@ -21,11 +22,14 @@ async def get_account_token(self, address, password) -> Token:
"""
https://docs.mail.tm/#authentication
"""
payload = {
headers = {
"accept": "application/ld+json",
"Content-Type": "application/json"
}
response = await self.session.post(f"{self.API_URL}/token", data=json.dumps({
"address": address,
"password": password
}
response = await self.session.post(f"{self.API_URL}/token", json=payload)
}), headers=headers)
logger.debug(f'Response for {self.API_URL}/token: {response}')
if await validate_response(response):
return Token(**(await response.json()))
Expand Down Expand Up @@ -71,7 +75,10 @@ async def get_account(self, address=None, password=None) -> Account:
response = await self.session.post(f"{self.API_URL}/accounts", json=payload)
logger.debug(f'Response for {self.API_URL}/accounts: {response}')
if await validate_response(response):
return Account(**(await response.json()))
response = await response.json()
token = await self.get_account_token(address=address, password=password)
response['token'] = token
return Account(**response)
logger.debug(f'Error response for {self.API_URL}/accounts: {response}')
raise MailTMInvalidResponse

Expand Down
11 changes: 6 additions & 5 deletions mailtm/schemas/account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from pydantic import BaseModel


class Token(BaseModel):
id: str
token: str


class Account(BaseModel):
id: str
address: str
Expand All @@ -10,8 +15,4 @@ class Account(BaseModel):
isDeleted: bool
createdAt: str
updatedAt: str


class Token(BaseModel):
id: str
token: str
token: Token
2 changes: 1 addition & 1 deletion mailtm/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def random_string(length=8):
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))


async def validate_response(response) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
zip_safe=False,
url='https://github.com/prtolem/MailTM',
install_requires=[
'aiohttp==3.8.1'
'aiohttp==3.9.2',
'pydantic==1.10.2',
])

0 comments on commit 632c507

Please sign in to comment.