uberVU/libsaas — pull request #1 by tiriplicamihai (2013-11-04 17:00:36+00:00)
Title: Add Pardot Service client

## Need

```gherkin
As a uberVU developer
I want to get and update Pardot prospects
In order to automate marketing campaigns
```
Since there is no available Python client for Pardot API, we will implement our own client.

## Deliverables

- [ ] A Pardot client through which we will get and update prospects

## Solution

The Pardot clinet must be implemented in [libsaas repo](https://github.com/uberVU/libsaas).

Add a directory in libsaas/sevices named pardot.

There create a service.py file which will contain the Pardot class which is the actual Pardot service client. This class must inherit ``` libsaas.services.base.Resource ```.

Because we need Authorization will take as example Github service. __init__ method should look like this:

```python
def __init__ (self, api_key_or_email, user_key, password=None):
# Pardot has a strange url schema: base_url/endpoint/version
self.apiroot = 'https://pi.pardot.com/api/{path}/version/3`

if password is None:
self.api_key = api_key_or_email
self.user_key = user_key
self.add_filter(self.add_api_key_authorization)
else:
self.add_filter(self.add_basic_authorization)
```

``` add_filter ``` method is used to now to add user credentials to the request. One example is this:

```python
def add_authorization(self, request):
request.params.update({
'access_token': self.access_token
})
```

In add_api_key_authorization the request should be updated with api_key and user_key and for add_basic_authorization email, password and user_key should be provided

Define a get_url method which receives path too and formats apiroot.

For now Pardot Client will be offer services for authorization and querying and updating prospects, so we need 3 methods in service class:

```python
@base.resource(AuthorizationResource)
def authorization(self):
return AuthorizationResource(self)

@base.resource(ProspectsResource)
def prospects(self):
return ProspectsResource(self)

@base.resource(ProspectResource)
def prospect(self):
""" for updated """
return ProspectResource(self)
```

Define a authorization resource in "authorization.py" file:

```python

class AuthorizationResource(PardotResource):
path = "login"
```

PardotResource must be implemented in resources.py and must inherit from ``` libsaas.services.base.RestResources ```. Because in Pardot documentation is recommended that all requests should be post only a post method should be implemented.

ProspectResource and ProspectsResource must be implemented in prospects.py and inherit from PardotResource.

```python
class ProspectsResource(PardotResource):
path = 'prospect'

@base.apimethod
def query(self, filters)
url = "{0}/{1}".format(self.get_url(self.path), "do/query/"
...
```

Same with ProspectResource, only there we define an update_method and the endpoint is "do/update/".

#### Prerequisites
- http://developer.pardot.com/
- http://developer.pardot.com/kb/api-version-3/querying-prospects
- http://developer.pardot.com/kb/api-version-3/using-prospects
- http://developer.pardot.com/kb/api-version-3/error-codes-and-messages
- https://github.com/uberVU/libsaas/tree/master/libsaas/services/github

#### TODO
- [ ] Add Pardot Service class
- [ ] Add authentication resource
- [ ] Add prospects resource
- [ ] Add prospect resource
- [ ] Provide libsaas

#### Thehole issue: https://github.com/uberVU/thehole/pull/9855