Package: mercadopago-result
MercadoPago ResultA simple interface with MercadoPago RESTFUL API. This package use the packages
Result, Pydantic and HTTPX.Pydanticmakes sure to validate the data sent and received by the HTTP
requests.Resultis a package that implements an imitation of the programming language
Rust for returning results, also put a good use for the Python statement
"match"HTTPXhandles all requesitions send to the API.Table of ContentsInstallationSummaryAccessTokenRequestsCheckoutCreateListGetPutClientCreateListGetPutInstallationLatest version$ pip install mercadopago-resultSummaryThe idea for this library was to not depend on JSON / Dict objects to be sent and
received to and from the MercadoPago API. With Pydantic model validation, it is
possible to be sure that the data you sent or received is what you expected.
And the Result library is a nice way to handle the Package requests results.To use this package it is necessary to get a MercadoPago API Token and create
an AccessToken object with it, then all API requests will require the token and
a object filter depending of what will be requested.AccessTokenExample creating the AccessToken:fromresultimportOk,Errfrommercadopago_result.authenticationimportAccessTokenfrommercadopago_result.authenticationimportAccessTokenErroraccess_token=NonematchAccessToken.load_from_json_file("token_file.json"):caseOk(access_token):# The token's file had no problems and now the access_token is setpasscaseErr(AccessTokenError.LoadingFile.FileDoesNotExist(path)):# The error above set the 'path' with the file path that it could not# findprint(f"File '{path}' does not exist")caseErr(AccessTokenError.Parsing(message)):# If the message didn't matter, just put a underscore _ instead of# 'message'.print(f"Invalid Json:{message}")# If everything was Ok in the match statement this should print the token modelprint(access_token)And this is an example to not deal with result library, in case it is not
desired to write an entire match statement:frommercadopago_result.authenticationimportAccessToken# This will raise an exception if the loading returned an Err instead of an Okaccess_token=AccessToken.load_from_json_file("token_file.json").unwrap()print(access_token)RequestsFor now the package only have works with MercadoPago API Checkout and Client.Here are some examples for Checkout requests.CheckoutCreateEndpoint "https://api.mercadopago.com/checkout/preferences"frompprintimportpprint# just for the examplefromresultimportOk,Errfrommercadopago_result.authorizationimportAccessTokenfrommercadopago_result.api_requests.checkoutimportsend_checkout_create_requestfrommercadopago_result.api_requests.checkoutimportCheckoutCreateErrorfrommercadopago_result.checkoutimportCheckoutfrommercadopago_result.checkoutimportItemListFieldfrommercadopago_result.checkoutimportCheckoutRedirectURLSaccess_token=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")checkout_items:ItemListField=[Item(title="Something nice",quantity=420,currency_id=ValidCurrencies.BRL,unit_price=69.0),# ...# Item(...)]checkout_redirect_urls=CheckoutRedirectURLS(success="my.site.com/success",failure="my.site.com/failure",pending="my.site.com/pending")checkout=Checkout(items=checkout_items,back_urls=checkout_redirect_urls,# same as "approved"auto_return=CheckoutAutoReturnOptions.APPROVED,notification_url="my.site.com/webhook")pprint(checkout.model_dump(exclude_unset=True))matchsend_checkout_create_request(checkout,access_token):caseOk(checkout_response_from_api):pprint(checkout_response_from_api.model_dump(exclude_unset=True))caseErr(CheckoutCreateError.HttpRequest(code,api_error_type,api_error_message)):print(code,api_error_type,api_error_message)caseErr(something_unexpected):print(something_unexpected)# This also have the same result but avoid the match statementcheckout=send_checkout_create_request(checkout,access_token).unwrap()pprint(checkout.model_dump(exclude_unset=True))ListEndpoint "https://api.mercadopago.com/checkout/preferences/search"frompprintimportpprintfrommercadopago_result.api_requests.checkoutimportsend_checkout_get_list_requestfrommercadopago_result.checkoutimportCheckoutfrommercadopago_result.checkoutimportCheckoutShortenfrommercadopago_result.authenticationimportAccessTokentoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")# A checkout that will be used as filtercheckout_filter=Checkout()checkout_list=send_checkout_get_list_request(checkout_filter,token).unwrap()pprint([c.model_dump(exclude_unset=True)forcincheckout_list])# the send_checkout_get_list_request returns a list of CheckoutShorten.# For some reason the API responds with different type of checkout for this# request, so it was necessary to create a second class to handle this. In# future this package might be adjusted to return a Checkout insteadGetEndpoint "https://api.mercadopago.com/checkout/preferences/{id}"frompprintimportpprintfrommercadopago_result.api_requests.checkoutimportsend_checkout_get_requestfrommercadopago_result.checkoutimportCheckoutfrommercadopago_result.authenticationimportAccessTokentoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")checkout_filter=Checkout(id="id")checkout_response=send_checkout_get_request(checkout_filter,token).unwrap()pprint(checkout_response.model_dump(exclude_unset=True))PutEndpoint "https://api.mercadopago.com/checkout/preferences/{id}"frompprintimportpprintfrommercadopago_result.api_requests.checkoutimportsend_checkout_put_requestfrommercadopago_result.checkoutimportCheckoutfrommercadopago_result.authenticationimportAccessTokentoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")checkout_filter=Checkout(id="id",)# id and other values to be updatedcheckout_response=send_checkout_put_request(checkout_filter,token).unwrap()pprint(checkout_response.model_dump(exclude_unset=True))ClientCreateEndpoint "https://api.mercadopago.com/v1/customers"frompprintimportpprintfrommercadopago_result.clientimportClientfrommercadopago_result.authenticationimportAccessTokenfrommercadopago_result.api_requests.clientimportsend_client_creation_requesttoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")client_info=Client()# fill the informationnew_client=send_client_creation_request(client_info,token)pprint(new_client.model_dump(exclude_unset=True))ListEndpoint "https://api.mercadopago.com/v1/customers/search"frompprintimportpprintfrommercadopago_result.clientimportClientfrommercadopago_result.authenticationimportAccessTokenfrommercadopago_result.api_requests.clientimportsend_client_get_list_requesttoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")client_filter=Client()# fill the informationclient_list=send_client_get_list_request(client_filter,token)pprint([c.model_dump(exclude_unset=True)forcinclient_list])GetEndpoint "https://api.mercadopago.com/v1/customers/search"Obs.: The actual endpoint if "https://api.mercadopago.com/v1/customers/{id}"
this package is using the search endpoint because both return the same client
object with the same amount of information. This might change in future to use
the proper endpointfrompprintimportpprintfrommercadopago_result.clientimportClientfrommercadopago_result.authenticationimportAccessTokenfrommercadopago_result.api_requests.clientimportsend_client_get_requesttoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")client_filter=Client()# fill the informationclient=send_client_get_request(client_filter,token)pprint(client.model_dump(exclude_unset=True))PutEndpoint "https://api.mercadopago.com/v1/customers/{id}"frompprintimportpprintfrommercadopago_result.clientimportClientfrommercadopago_result.authenticationimportAccessTokenfrommercadopago_result.api_requests.clientimportsend_client_put_requesttoken=AccessToken(public="YOUR_PUBLIC_API_TOKEN",private="YOUR_PRIVATE_API_TOKEN")client_update_data=Client()# fill the informationclient=send_client_put_request(client_update_data,token)pprint(client.model_dump(exclude_unset=True))