Usage¶
To use reporting-api-client in a project:
from reporting_api_client import ReportingAPIClient
In order to instantiate the client you must provide 2 mandatory parameters, your client_id
and
client_secret
. If you don’t have this credentials, follow the instructions on the
Authentication section of the Reporting API documentation.
Once you have your hands on a credential pair, you can do something like:
from reporting_api_client import ReportingAPIClient
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
client = ReportingAPIClient(CLIENT_ID, CLIENT_SECRET)
The client has 2 method major methods for query the API:
reporting_api_client.ReportingAPIClient.query()
reporting_api_client.ReportingAPIClient.pivot()
The query
method executes a raw query against the API, returning the results in dict
form if
the request is successfull and raising the corresponding errors if the request fails. It could be
used the following way:
query = """
pivot(from: "2022-09-01", to: "2022-09-02") {
results {
clicks
}
}
"""
print(client.execute(query))
This method also receives any keyword argument accepted by the gql.client.execute method.
The pivot
method will build a query according to the parameters, allowing you to define the
parameters of the query instead of building it yourself. In order to replicate the query on the
previous example, one would have to do the following:
import datetime as dt
client.pivot(
from=datetime.datetime(2022, 9, 1),
to=datetime.datetime(2022, 9, 2),
metrics=["clicks"],
)
Warning
In order to use the reporting_api_client.ReportingAPIClient.pivot()
method you must install
the library with the pandas optional. More information on Installation.