Contract testing in API body with Python - python

Does anyone have an example of how I can test an api with python as long as I know the structure under the following conditions: If the field is an integer or String; If the field can receive Null, if it is mandatory, if the length is < or > that....
I searched for some articles not found on Python, only on Ruby.

Related

Query Elasticsearch using Python to get all "used" fields in an index

I want to programmatically (w/ Python 3.10.5) query an ElasticSearch (v7.16.2) index and receive a list of fields identical to what Kibana would show under "Available Fields" for the index.
I've seen a few versions of this question asked. I think one answer involved an API call that is no longer available. I tried the "Field usage stats API" but there were always fields missing. Using the "Get field mapping API" returns everything in the mapping which is way too broad. A couple of answers have suggested that it isn't possible to do this, which makes me curious as to how Kibana knows which fields are available for each index.
These are the lines of code I used to pull the fields from each index.
field_usage_stats = es.indices.field_usage_stats(index=index) #too few fields returned
field_mapping = es.indices.get_mapping(index=index) #too many fields returned
I reviewed the raw data returned by each of these methods to ensure the issue wasn't an error in my parsing.
https://www.elastic.co/guide/en/elasticsearch/reference/7.16/field-usage-stats.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.16/indices-get-mapping.html

Strange! python datetime.datetime.now() randomly generate unix second and nanosecond timestamp

I have deployed a GCP cloud function that updates Firestore time_created and time_updated fields in Firestore. My front-end app first creates these fields in Firestore but my function updates them after processing the documents. A snippet of code below generates the timestamp and I use Firestore update function to update the document. There are a few instances where the fields in Firestore will be updated as a dictionary with keys as "seconds" and "nano_seconds" and their values but not as Timestamp. I have been wondering and trying to track down where the issue is coming from. I suspect datetime.now() sometimes does not generate a timestamp value. Help me if you have an idea or seen something like this before. I have attached a snapshot below. The image attached shows an instance of the wrongly formatted date returned from Firestore to my Front-end.
Documents affected have the field showing as this:
time_created: {'seconds': 1637694047.0, 'nanoseconds': 580592000.0}
from datetime import datetime
update_doc = {
u"time_created": datetime.now(),
u"time_updated": datetime.now()
}
Per #mark-tolonen, please don't include images in questions when it's trivial to copy-and-paste the test. Various reasons.
I experienced a different issue with Firestore timestamps and using the Go SDK. When I read your question, I wondered if the issues were related but, I think not.
That said, you can perform some diagnosis. You can emit the Python datetime.now() values of course to ensure you know what's being applied.
You could (!) then use the underlying REST API directly to mimic|repro the calls that your code is making to determine whether the error arises in the API itself or the Python SDK (or your code).
Here's projects.databases.documents.patch which I think underlies the Set. There's also projects.databases.documents.create. In both cases, APIs Explorer provides a way for you to try the API methods in the browser and will yield the e.g. curl equivalents for you.
NOTE
The API requires a parent parameter, defined to be something of the form projects/{project_id}/databases/{databaseId}/documents. Replace project_id with your Project ID and use (default) (with the parenthesis) for the value of {databaseId}.

Python Wordpress REST API returns False when passing an array as a value

decided to make a project that uses ACF(Wordpress), Wordpress REST API and Python.
I have a custom field type. Whenever I try to POST an array as a value, I get a JSON response with the response of False.
This custom field is a gallery, thus it has an array of links. It’s just that I cannot update that list but I can pass everything else(like strings).
I’m aware than the POST data should use JSON argument and I’ve tried it with other types of variables. I’m aware that the list needs to be passed as a string, but still no luck.
I’m using JWT as authorisation and my headers are:
curHeaders = {
“Authorization”: “Bearer %s” % jwt_token,
“Content-Type”: “application/json”,
“Accept”: “application/json”
}

Jira-Python project key

I'm using jira-python to access jira projects.
I'm trying to pass a numeric key (which is not the project id) as a parameter.
myProject = jira.project("100")
However, I get the following error:
response text = {"errorMessages":["No project could be found with id '100'."],"errors":{}}
I'm not trying to pass an id, so is there a way to differentiate between a numeric key and an id.
I was doing some testing and it seems like an alphanumeric key is accepted e.g. key = Test1
According to Jira Rest API documentation, Jira client always expects to receive a projectIdOrKey parameter as string. And it seems so, that the only way for it to determine, whether a Project Key or a Project ID was passed, is to check whether a string is numeric or not. That would mean that there is no way for Jira to recognise that you are using a Project Key instead of a Project ID, if you are passing a numerical value.
I checked that the source code of Jira API client in Python does indeed convert any input into string. Unfortunately, I can't check whether Jira API server indeed decides on whether it received an ID or a key in a way I suppose it does because it is not open source.
If my hypothesis is correct, then the only solution would seem to be not to use numerical values as project keys, instead always using a key that contains letters as well.
You could not create a project in Jira with the key beginning with the number.
Project keys must start with an uppercase letter, followed by one or more uppercase alphanumeric characters.
https://support.atlassian.com/jira-work-management/docs/work-with-issues-in-jira-cloud/#Workingwithissues-Projectkeys
Project admins can create and assign their project's key when they create a new project. Based on the project's name, Jira suggests a recognizable key. If you're a project admin, you can customize this while creating a project by selecting Advanced options. You can also update it in the project's settings. They must be at least 2 characters long and start with an uppercase letter. Read more about editing a project's details.

API id - Convert ids into integer

I'm playing with API and it throw me this kind of id : ff869d1f-0923-4d28-8577-4c36291f0fca
I wonder if the id is encoded in a specific format and if I can convert it into an integer, working on python.
Your ID is a UUID. Technically it is a 128bit integer, but since such long numbers are rather impractical to use they are mostly presented as dash-separated hexadecimal groups like your ff869d1f-0923-4d28-8577-4c36291f0fca.
Python as well as modern databases offer direct support for UUIDs. In most cases it is not helpful to convert them.
Exact details about UUIDs, their types (versions), fields and conversion options can be found in the documentation of the UUID module.

Categories

Resources