Create "Tutorials" category for Copilot (#56317)
Co-authored-by: Sarah Schneider <sarahs@github.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
---
|
||||
title: Debugging invalid JSON
|
||||
shortTitle: Debug invalid JSON
|
||||
intro: '{% data variables.copilot.copilot_chat_short %} can identify and resolve syntax errors or structural issues in JSON data.'
|
||||
redirect_from:
|
||||
- /copilot/example-prompts-for-github-copilot-chat/debugging-errors/debugging-invalid-json
|
||||
- /copilot/copilot-chat-cookbook/debugging-errors/debugging-invalid-json
|
||||
versions:
|
||||
feature: copilot
|
||||
category:
|
||||
- Debugging code
|
||||
complexity:
|
||||
- Intermediate
|
||||
octicon: bug
|
||||
topics:
|
||||
- Copilot
|
||||
---
|
||||
|
||||
When working with JSON data, you may encounter issues such as trailing commas, mismatched braces, or incorrect data types that make the JSON invalid. {% data variables.copilot.copilot_chat %} can help you debug and fix these errors by suggesting corrections to fix invalid JSON.
|
||||
|
||||
## Example scenario
|
||||
|
||||
Consider a scenario where an application consumes JSON data from an API, but the response fails to parse due to invalid formatting. You receive the error message:
|
||||
|
||||
```bash
|
||||
Error: Parse error
|
||||
----------------------^
|
||||
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
|
||||
```
|
||||
|
||||
Below is the JSON data that caused the error:
|
||||
|
||||
```json
|
||||
{
|
||||
"location": "San Francisco",
|
||||
"current_weather": {
|
||||
"temperature": 18,
|
||||
"unit": "Celsius",
|
||||
"conditions": "Cloudy
|
||||
},
|
||||
"forecast": {
|
||||
"day": "Monday",
|
||||
"high": 22,
|
||||
"low": 15,
|
||||
"precipitation": 10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Example prompt
|
||||
|
||||
`Why is my JSON object invalid and how can I fix it?`
|
||||
|
||||
## Example response
|
||||
|
||||
{% data reusables.copilot.example-prompts.response-is-an-example %}
|
||||
|
||||
{% data variables.product.prodname_copilot_short %} might suggest that your JSON is invalid because it's missing a closing quote for the `conditions` value. Here is the corrected JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"location": "San Francisco",
|
||||
"current_weather": {
|
||||
"temperature": 18,
|
||||
"unit": "Celsius",
|
||||
"conditions": "Cloudy"
|
||||
},
|
||||
"forecast": {
|
||||
"day": "Monday",
|
||||
"high": 22,
|
||||
"low": 15,
|
||||
"precipitation": 10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this example response, {% data variables.product.prodname_copilot_short %}'s suggestions include fixing the closing quote for the `conditions` value, which resolves the JSON parsing error.
|
||||
|
||||
## Further reading
|
||||
|
||||
{% data reusables.copilot.example-prompts.further-reading-items %}
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
title: Handling API rate limits
|
||||
shortTitle: Handle API rate limits
|
||||
intro: '{% data variables.copilot.copilot_chat_short %} can help handle API rate limits by suggesting code that detects implements retry logic.'
|
||||
redirect_from:
|
||||
- /copilot/example-prompts-for-github-copilot-chat/debugging-errors/handling-api-rate-limits
|
||||
- /copilot/copilot-chat-cookbook/debugging-errors/handling-api-rate-limits
|
||||
versions:
|
||||
feature: copilot
|
||||
category:
|
||||
- Debugging code
|
||||
complexity:
|
||||
- Intermediate
|
||||
octicon: bug
|
||||
topics:
|
||||
- Copilot
|
||||
---
|
||||
|
||||
When making requests to APIs, it's common to encounter rate limits that restrict the number of calls you can make within a certain time frame. {% data variables.copilot.copilot_chat %} can help you handle these limits by suggesting code to detect rate limit responses and automatically retry requests after a delay.
|
||||
|
||||
## Example scenario
|
||||
|
||||
The following Python code fetches weather data from an external API. If the API has rate limits, requests may fail when limits are exceeded, and your app may need a way to handle these responses gracefully by implementing retry logic.
|
||||
|
||||
```python
|
||||
from flask import Flask, request
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
WEATHER_API_URL = "https://api.example.com/weather"
|
||||
|
||||
@app.route('/get_weather', methods=['GET'])
|
||||
def get_weather():
|
||||
city = request.args.get('city')
|
||||
# Simulate an API request to the external weather service
|
||||
response = requests.get(WEATHER_API_URL, params={"city": city})
|
||||
weather_data = response.json()
|
||||
|
||||
return weather_data
|
||||
```
|
||||
|
||||
## Example prompt
|
||||
|
||||
`How can I handle API rate limits within get_weather().`
|
||||
|
||||
## Example response
|
||||
|
||||
{% data reusables.copilot.example-prompts.response-is-an-example %}
|
||||
|
||||
{% data variables.product.prodname_copilot_short %} might suggest code that implements a retry mechanism with exponential backoff to limit the frequency of retry attempts.
|
||||
|
||||
For example:
|
||||
|
||||
```python
|
||||
import requests
|
||||
from flask import Flask, request
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.packages.urllib3.util.retry import Retry
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
WEATHER_API_URL = "https://api.example.com/weather"
|
||||
|
||||
def requests_retry_session(
|
||||
retries=3,
|
||||
backoff_factor=0.3,
|
||||
status_forcelist=(500, 502, 504),
|
||||
session=None,
|
||||
):
|
||||
session = session or requests.Session()
|
||||
retry = Retry(
|
||||
total=retries,
|
||||
read=retries,
|
||||
connect=retries,
|
||||
backoff_factor=backoff_factor,
|
||||
status_forcelist=status_forcelist,
|
||||
)
|
||||
adapter = HTTPAdapter(max_retries=retry)
|
||||
session.mount("http://", adapter)
|
||||
session.mount("https://", adapter)
|
||||
return session
|
||||
|
||||
@app.route('/get_weather', methods=['GET'])
|
||||
def get_weather():
|
||||
city = request.args.get('city')
|
||||
try:
|
||||
response = requests_retry_session().get(WEATHER_API_URL, params={"city": city})
|
||||
response.raise_for_status()
|
||||
weather_data = response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
return {"error": str(e)}, 500
|
||||
|
||||
return weather_data
|
||||
```
|
||||
|
||||
In this example, {% data variables.product.prodname_copilot_short %}'s suggestions include setting up a retry session that allows the code to automatically retry requests if they fail due to specific status codes (500, 502, 504). The `backoff_factor` gradually increases the delay between retries, helping avoid exceeding the API's rate limit further.
|
||||
|
||||
## Further reading
|
||||
|
||||
{% data reusables.copilot.example-prompts.further-reading-items %}
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
title: Debugging errors
|
||||
intro: 'Discover ways that you can use {% data variables.product.prodname_copilot %} to debug errors during development.'
|
||||
redirect_from:
|
||||
- /copilot/example-prompts-for-github-copilot-chat/debugging-errors
|
||||
- /copilot/copilot-chat-cookbook/debugging-errors
|
||||
versions:
|
||||
feature: copilot
|
||||
topics:
|
||||
- Copilot
|
||||
children:
|
||||
- /debugging-invalid-json
|
||||
- /handling-api-rate-limits
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user