<commit_before>import os
import pycurl
import StringIO
import json
from couchdbkit.ext.django.schema import *
from urllib import urlencode
from corehq.apps.sms.util import clean_phone_number
from corehq.apps.sms.mixin import SMSBackend
from corehq.apps.telerivet.forms import TelerivetBackendForm

MESSAGE_TYPE_SMS = "sms"

class TelerivetBackend(SMSBackend):
    api_key = StringProperty()      # The api key of the account to send from.
    project_id = StringProperty()   # The Telerivet project id.
    phone_id = StringProperty()     # The id of the phone to send from, as shown on Telerivet's API page.
    webhook_secret = StringProperty() # The Webhook Secret that gets posted to hq on every request

    class Meta:
        app_label = "telerivet"

    @classmethod
    def get_api_id(cls):
        return "TELERIVET"

    @classmethod
    def get_generic_name(cls):
        return "Telerivet (Android)"

    @classmethod
    def get_template(cls):
        return "telerivet/backend.html"

    @classmethod
    def get_form_class(cls):
        return TelerivetBackendForm

    def send(self, msg, *args, **kwargs):
        text = msg.text.encode("utf-8")
        params = urlencode({
            "phone_id" : str(self.phone_id),
            "to_number" : clean_phone_number(msg.phone_number),
            "content" : text,
            "message_type" : MESSAGE_TYPE_SMS,
        })
        url = "https://api.telerivet.com/v1/projects/%s/messages/outgoing" % str(self.project_id)
        
        curl = pycurl.Curl()
        buf = StringIO.StringIO()
        
        curl.setopt(curl.URL, url)
        curl.setopt(curl.USERPWD, "%s:" % str(self.api_key))
        curl.setopt(curl.WRITEFUNCTION, buf.write)
        curl.setopt(curl.POSTFIELDS, params)
        curl.setopt(curl.CAINFO, "%s/cacert.pem" % os.path.dirname(os.path.abspath(__file__)))
        curl.perform()
        curl.close()
        
        result = json.loads(buf.getvalue())
        buf.close()

    @classmethod
    def by_webhook_secret(cls, webhook_secret):
        return cls.view("telerivet/backend_by_secret", key=[webhook_secret], include_docs=True).one()
<commit_msg>have telerivit send use requests instead of pycurl<commit_after>import os
import requests
from couchdbkit.ext.django.schema import *
from corehq.apps.sms.util import clean_phone_number
from corehq.apps.sms.mixin import SMSBackend
from corehq.apps.telerivet.forms import TelerivetBackendForm

MESSAGE_TYPE_SMS = "sms"


class TelerivetBackend(SMSBackend):
    # The api key of the account to send from.
    api_key = StringProperty()
    # The Telerivet project id.
    project_id = StringProperty()
    # The id of the phone to send from, as shown on Telerivet's API page.
    phone_id = StringProperty()
    # The Webhook Secret that gets posted to hq on every request
    webhook_secret = StringProperty()

    class Meta:
        app_label = "telerivet"

    @classmethod
    def get_api_id(cls):
        return "TELERIVET"

    @classmethod
    def get_generic_name(cls):
        return "Telerivet (Android)"

    @classmethod
    def get_template(cls):
        return "telerivet/backend.html"

    @classmethod
    def get_form_class(cls):
        return TelerivetBackendForm

    def send(self, msg, *args, **kwargs):
        text = msg.text.encode("utf-8")
        params = {
            "phone_id": str(self.phone_id),
            "to_number": clean_phone_number(msg.phone_number),
            "content": text,
            "message_type": MESSAGE_TYPE_SMS,
        }
        url = "https://api.telerivet.com/v1/projects/%s/messages/outgoing" % str(self.project_id)

        result = requests.post(
            url,
            auth=(str(self.api_key), ''),
            data=params,
            cert="%s/cacert.pem" % os.path.dirname(os.path.abspath(__file__)),
        )

        result = result.json()

    @classmethod
    def by_webhook_secret(cls, webhook_secret):
        return cls.view("telerivet/backend_by_secret", key=[webhook_secret],
                        include_docs=True).one()
{'fraction_non_alphanumeric': 0.10283600493218249, 'fraction_numerical': 0.0009864364981504315, 'mean_word_length': 2.7486136783733826, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7197283', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1316, 'n_words': 288}