|
1 | | - |
| 1 | +# Search code api endpoints |
| 2 | +from searchcode.connection import __api_handler |
| 3 | +from searchcode.miscellaneous import __code_sources, __code_languages, __api_endpoints |
| 4 | + |
| 5 | + |
| 6 | +def __join_parameters(names, sources) -> str: |
| 7 | + """ |
| 8 | + Returns a single string of joined parameters for the given list of filter names and sources. |
| 9 | +
|
| 10 | + Parameters: |
| 11 | + ----------- |
| 12 | + names (list): |
| 13 | + A list of names used as filters. |
| 14 | + sources (list): |
| 15 | + A list of tuples representing the sources to be filtered. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + -------- |
| 19 | + str: A single string of joined parameters for the given list of filter names. |
| 20 | +
|
| 21 | + About |
| 22 | + ----- |
| 23 | + The __join_parameters function takes a list of names and sources as input and iterates over each name. |
| 24 | + For each name, it iterates over the sources list and compares the lowercase version of the |
| 25 | + source name to the lowercase version of each source name in the sources list. |
| 26 | + If a match is found, the corresponding parameter is added to the parameters string, |
| 27 | + and the loop breaks out of the inner loop to move on to the next source name. |
| 28 | + After iterating over all the source names, the function returns the parameters string. |
| 29 | + """ |
| 30 | + parameters = "" |
| 31 | + for name in names: |
| 32 | + for source_name, source_parameter in sources: |
| 33 | + if source_name.lower() == name.lower(): |
| 34 | + parameters += source_parameter |
| 35 | + break |
| 36 | + |
| 37 | + return parameters |
| 38 | + |
| 39 | + |
| 40 | +def code_search(query: str, page_number: int = 0, per_page: int = 10, max_retries: int = 3, backoff_time: int = 10, |
| 41 | + code_sources: list = None, code_languages: list = None) -> list: |
| 42 | + """ |
| 43 | + Queries the code index and returns at most 10 results. |
| 44 | + If the results list is empty, then this indicates that you have reached the end of the available results. |
| 45 | + To fetch all results for a given query, keep incrementing 'page_number' |
| 46 | + until you get a page with an empty results list. |
| 47 | +
|
| 48 | + Parameters: |
| 49 | + ---------- |
| 50 | + query (str): |
| 51 | + The search query string. |
| 52 | + page_number (int): |
| 53 | + Result page starting at 0 through to 49. |
| 54 | + per_page (int): |
| 55 | + Number of results wanted per page max 100. |
| 56 | + max_retries (int): |
| 57 | + The maximum number of retries in case of a failed request. |
| 58 | + backoff_time (int): |
| 59 | + The number of seconds to wait before retrying a failed request. |
| 60 | + code_sources : list |
| 61 | + A list of source names used as filters. |
| 62 | + code_languages : list |
| 63 | + A list of programming language names used as filters. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + ------- |
| 67 | + list: A list of code search results. |
| 68 | +
|
| 69 | + About |
| 70 | + ----- |
| 71 | + The code_search function queries the code index by sending an HTTP request to the code_search API endpoint. |
| 72 | + It accepts several optional parameters, including query, page_number, per_page, max_retries, backoff_time, |
| 73 | + code_sources, and code_languages. |
| 74 | + The sources and languages lists are filtered by calling the __join_parameters function, |
| 75 | + which returns a single string of joined parameters based on the filter names and sources. |
| 76 | + The function then calls the __api_handler function to handle the API request, which returns a dictionary of results. |
| 77 | + The results list is then extracted and returned. |
| 78 | + If the results list is empty, this indicates that you have reached the end of the available results. |
| 79 | + To fetch all results for a given query, keep incrementing 'page_number' until you get a page with an empty results list. |
| 80 | + """ |
| 81 | + if code_languages is None: |
| 82 | + code_languages = [] |
| 83 | + if code_sources is None: |
| 84 | + code_sources = [] |
| 85 | + |
| 86 | + code_search_endpoint = __api_endpoints()[0] |
| 87 | + sources = __join_parameters(code_sources, __code_sources()) |
| 88 | + languages = __join_parameters(code_languages, __code_languages()) |
| 89 | + |
| 90 | + return __api_handler(code_search_endpoint.format(query, page_number, per_page, sources, languages), |
| 91 | + max_retries, backoff_time)['results'] |
| 92 | + |
| 93 | + |
| 94 | +def code_result(code_id: int = None, max_retries: int = 3, backoff_time: int = 10) -> str: |
| 95 | + """ |
| 96 | + Retrieves the raw data for a code file based on the given code ID. |
| 97 | +
|
| 98 | + Parameters: |
| 99 | + ----------- |
| 100 | + code_id (int): |
| 101 | + The ID of the code file to retrieve. |
| 102 | + max_retries (int):, optional |
| 103 | + The maximum number of times to retry the API call in case of a failure. Defaults to 3. |
| 104 | + backoff_time (int):, optional |
| 105 | + The number of seconds to wait before retrying the API call in case of a failure. Defaults to 5. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + -------- |
| 109 | + str: The raw data for the code file, if the API call is successful. |
| 110 | +
|
| 111 | + About |
| 112 | + ----- |
| 113 | + This function takes a `code_id` parameter which is the ID of a code file as returned by a code_search |
| 114 | + result. It then sends a request to the corresponding API endpoint to retrieve the raw data for that |
| 115 | + code file. If the request is successful, the raw data is returned. |
| 116 | + """ |
| 117 | + code_result_endpoint = __api_endpoints()[1] |
| 118 | + return __api_handler(code_result_endpoint.format(code_id), max_retries, backoff_time)['code'] |
| 119 | + |
| 120 | + |
| 121 | +def related_results(code_id: int = None, max_retries: int = 3, backoff_time: int = 10) -> list: |
| 122 | + """ |
| 123 | + Queries the related code results endpoint of the Searchcode API and returns an array of results |
| 124 | + that are considered to be duplicates of the code file identified by the given code_id. |
| 125 | +
|
| 126 | + Parameters: |
| 127 | + ----------- |
| 128 | + code_id (int): |
| 129 | + The unique code id of the code file for which to retrieve related results. |
| 130 | + max_retries (int): |
| 131 | + The maximum number of times to retry the API call in case of failures. Default is 3. |
| 132 | + backoff_time (int): |
| 133 | + The time in seconds to wait between retries in case of failures. Default is 5. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + -------- |
| 137 | + list: A list of related results, where each result is a dictionary containing metadata |
| 138 | + about a code file, such as its filename, repository, and lines of code. |
| 139 | + """ |
| 140 | + related_results_endpoint = __api_endpoints()[2] |
| 141 | + return __api_handler(related_results_endpoint.format(code_id), max_retries, backoff_time) |
0 commit comments