Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
leshchenko1979 committed Oct 14, 2020
2 parents f8ebc2d + 7298588 commit 9942239
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
5 changes: 2 additions & 3 deletions fast_bitrix24/mult_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import more_itertools
import php
from .utils import http_build_query
import itertools

from .srh import BITRIX_MAX_BATCH_SIZE
Expand All @@ -25,13 +25,12 @@ async def run(self):

def prepare_batches(self):
batch_size = BITRIX_MAX_BATCH_SIZE
builder = php.Php()

batches = [{
'halt': 0,
'cmd': {
self.batch_command_label(i, item):
f'{self.method}?{builder.http_build_query(item)}'
f'{self.method}?{http_build_query(item)}'
for i, item in enumerate(next_batch)
}}
for next_batch in more_itertools.chunked(self.item_list, batch_size)
Expand Down
82 changes: 80 additions & 2 deletions fast_bitrix24/test_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .utils import http_build_query
import os

import pytest
Expand Down Expand Up @@ -86,7 +87,7 @@ def test_long_task_description(self, get_test):
b.get_by_ID('crm.lead.delete', [lead_no])


class TestEmbeddedListsInParams:
class TestParamsEncoding:

def test_mobile_phone(self, get_test):
b = get_test
Expand All @@ -104,4 +105,81 @@ def test_mobile_phone(self, get_test):
try:
assert lead['PHONE'][0]['VALUE_TYPE'] == 'MOBILE'
finally:
b.get_by_ID('crm.lead.delete', [lead_no])
b.get_by_ID('crm.lead.delete', [lead_no])


def test_filter_not_equal(self, create_100_leads):
b = create_100_leads

result = b.get_all('crm.lead.list', {
'FILTER': {
'!STATUS_ID': 'NEW'
}
})
assert not result

result = b.get_all('crm.lead.list', {
'FILTER': {
'!STATUS_ID': 'CLOSED'
}
})
assert result

result = b.get_all('crm.lead.list', {
'FILTER': {
'<>STATUS_ID': 'NEW'
}
})
assert result

result = b.get_all('crm.lead.list', {
'FILTER': {
'<>STATUS_ID': 'CLOSED'
}
})
assert result


class TestHttpBuildQuery:

def test_original(self):
assert http_build_query ({"alpha": "bravo"}) == "alpha=bravo&"

test = http_build_query({"charlie": ["delta", "echo", "foxtrot"]})
assert "charlie[0]=delta" in test
assert "charlie[1]=echo" in test
assert "charlie[2]=foxtrot" in test

test = http_build_query({
"golf": [
"hotel",
{"india": "juliet", "kilo": ["lima", "mike"]},
"november", "oscar"
]
})
assert "golf[0]=hotel" in test
assert "golf[1][india]=juliet" in test
assert "golf[1][kilo][0]=lima" in test
assert "golf[1][kilo][1]=mike" in test
assert "golf[2]=november" in test
assert "golf[3]=oscar" in test


def test_new(self):
d = {
'FILTER': {
'STATUS_ID': 'CLOSED'
}
}

test = http_build_query(d)
assert test == 'FILTER[STATUS_ID]=CLOSED&'

d = {
'FILTER': {
'!STATUS_ID': 'CLOSED'
}
}

test = http_build_query(d)
assert test == 'FILTER[%21STATUS_ID]=CLOSED&'
31 changes: 29 additions & 2 deletions fast_bitrix24/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#
##########################################

import urllib
from urllib.parse import quote, urlparse

def _url_valid(url):
try:
result = urllib.parse.urlparse(url)
result = urlparse(url)
return all([result.scheme, result.netloc, result.path])
except:
return False
Expand All @@ -19,3 +19,30 @@ def _merge_dict(d1, d2):
if d2:
d3.update(d2)
return d3


def http_build_query(params, convention="%s"):
if len(params) == 0:
return ""

output = ""
for key in params.keys():

if type(params[key]) is dict:

output += http_build_query(params[key], convention % key + "[%s]")

elif type(params[key]) is list:

new_params = {str(i): element for i, element in enumerate(params[key])}

output += http_build_query(
new_params, convention % key + "[%s]")

else:

val = quote(str(params[key]))
key = quote(key)
output = output + convention % key + "=" + val + "&"

return output
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
aiohttp
asyncio
tqdm
more_itertools
php
more_itertools

0 comments on commit 9942239

Please sign in to comment.