Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Learning Notes #49 – Pitfall of Implicit Default Values in APIs

9 January 2025 at 14:00

Today, we faced a bug in our workflow due to implicit default value in an 3rd party api. In this blog i will be sharing my experience for future reference.

Understanding the Problem

Consider an API where some fields are optional, and a default value is used when those fields are not provided by the client. This design is common and seemingly harmless. However, problems arise when,

  1. Unexpected Categorization: The default value influences logic, such as category assignment, in ways the client did not intend.
  2. Implicit Assumptions: The API assumes a default value aligns with the client’s intention, leading to misclassification or incorrect behavior.
  3. Debugging Challenges: When issues occur, clients and developers spend significant time tracing the problem because the default behavior is not transparent.

Here’s an example of how this might manifest,


POST /items
{
  "name": "Sample Item",
  "category": "premium"
}

If the category field is optional and a default value of "basic" is applied when it’s omitted, the following request,


POST /items
{
  "name": "Another Item"
}

might incorrectly classify the item as basic, even if the client intended it to be uncategorized.

Why This is a Code Smell

Implicit default handling for optional fields often signals poor design. Let’s break down why,

  1. Violation of the Principle of Least Astonishment: Clients may be unaware of default behavior, leading to unexpected outcomes.
  2. Hidden Logic: The business logic embedded in defaults is not explicit in the API’s contract, reducing transparency.
  3. Coupling Between API and Business Logic: When defaults dictate core behavior, the API becomes tightly coupled to specific business rules, making it harder to adapt or extend.
  4. Inconsistent Behavior: If the default logic changes in future versions, existing clients may experience breaking changes.

Best Practices to Avoid the Trap

  1. Make Default Behavior Explicit
    • Clearly document default values in the API specification (but we still missed it.)
    • For example, use OpenAPI/Swagger to define optional fields and their default values explicitly
  2. Avoid Implicit Defaults
    • Instead of applying defaults server-side, require the client to explicitly provide values, even if they are defaults.
    • This ensures the client is fully aware of the data being sent and its implications.
  3. Use Null or Explicit Indicators
    • Allow optional fields to be explicitly null or undefined, and handle these cases appropriately.
    • In this case, the API can handle null as “no category specified” rather than applying a default.
  4. Fail Fast with Validation
    • Use strict validation to reject ambiguous requests, encouraging clients to provide clear inputs.

{
  "error": "Field 'category' must be provided explicitly."
}

5. Version Your API Thoughtfully:

  • Document changes and provide clear migration paths for clients.
  • If you must change default behaviors, ensure backward compatibility through versioning.

Implicit default values for optional fields can lead to unintended consequences, obscure logic, and hard-to-debug issues. Recognizing this pattern as a code smell is the first step to building more robust APIs. By adopting explicitness, transparency, and rigorous validation, you can create APIs that are easier to use, understand, and maintain.

Connect postman to salesforce

3 January 2025 at 16:27

Today, I want to capture notes that I learnt from trailhead academy on connecting postman to a salesforce org.

To make postman allow changes at Salesforce org, we have to enable CORS policy in Salesforce. See below what does CORS mean.

CORS- Cross Origin Resource Sharing

It is a browser feature that controls how resources are requested from one site to another site. By configuring CORS, it enables special permissions for other external websites to access our salesforce data. In this case, we are enabling CORS for postman to access salesforce.

  • From setup ==> search for CORS ==> Add https://*.postman.co and https://*.postman.com URL
  • After that, in postman desktop -Do below steps one by one.
  • Create a separate workspace for Salesforce APIs to play around.
  • Search for Salesforce APIs. It does list out all the available collections.
  • Fork “Salesforce Platform API” and it will available to your local postman workspace.
  • After that, go to “Authorization” click on “Generate token” and copy “instance” URL.
  • Configure “_endpoint” value from variable tab as “instance” URL
  • All set and that’s it. You can play around whatever requests that are available.

Connect postman to salesforce

3 January 2025 at 16:27

Today, I want to capture notes that I learnt from trailhead academy on connecting postman to a salesforce org.

To make postman allow changes at Salesforce org, we have to enable CORS policy in Salesforce. See below what does CORS mean.

CORS- Cross Origin Resource Sharing

It is a browser feature that controls how resources are requested from one site to another site. By configuring CORS, it enables special permissions for other external websites to access our salesforce data. In this case, we are enabling CORS for postman to access salesforce.

  • From setup ==> search for CORS ==> Add https://*.postman.co and https://*.postman.com URL
  • After that, in postman desktop -Do below steps one by one.
  • Create a separate workspace for Salesforce APIs to play around.
  • Search for Salesforce APIs. It does list out all the available collections.
  • Fork “Salesforce Platform API” and it will available to your local postman workspace.
  • After that, go to “Authorization” click on “Generate token” and copy “instance” URL.
  • Configure “_endpoint” value from variable tab as “instance” URL
  • All set and that’s it. You can play around whatever requests that are available.

Using Google Sheets as a makeshift Database [Depriciated]

By: ashish
9 March 2020 at 19:51

Do you want need a quick solution without going into the hassle of setting up a Database? If your answer to any of those questions was a yes, then you’ve come to the right place. This post will show you how you can use Google sheets as your database.

For the purposes of this blogpost I will be usiing this Google sheet.

As you can see, we will be collecting the following data from the user – Name, Email and Age.

Create the API

  • Go to the google sheet you want to use.
  • Create column headers in the first column
  • Click on tools> script editor
  • Copy the following code to the editor

    • Click on run>run function> setup.
    • Now publish your script to get the request URL with the following settings.

Now let us test this URL in a webpage.

See the Pen
Simple register form
by Thomas Ashish Cherian (@pandawhocodes)
on CodePen.

You can enter your details here to see your details being updated in the Google Sheet above( refresh to see changes) .

A simple guide to building REST API’s in GO

By: ashish
18 June 2019 at 11:30

In this post we will build  simple REST API’s using the Go programming language. We will also be using the MUX Router. I will also explain some of the fundamentals of the language for beginners.

If you want to learn Go visit awesome-go-in-education. A curated list of resources about Go in Education. If you want to do the same but in Python read A simple guide to creating REST API’s with flask. I will be using Goland from jetbrains as my IDE.

Before we get started, a few jargon.

REST: a RESTful API uses HTTP requests to GET, PUT, POST and DELETE data.

RESTful API designing: guidelines is a must read before you continue. It talks about terminologies, endpoints, versioning status codes and so much more.

Test your environment

Let us first test the environment to check if everything is working fine. For that we will be using a simple “Hello World” program.

Running “Hello World” program

Once that is done, let us import necessary packages.

Performing imports

Let us look at the imports used one by one.

  1. encoding/json – since our API’s communications will be handled in JSON format
  2. log – will log errors
  3. net/http – We will use this package to create the API’s and communicate using HTTP protocols.
  4. mux –  A powerful URL router and dispatcher for golang . A router is used to define which function will run when a particular endpoint(URL) is called.

Writing the main funciton

Do note  In Go, := is for declaration + assignment, whereas = is for assignment only.For example, var foo int = 10 is the same as foo := 10.

  1. First we create a new variable for our multiplexer.
  2. Then we use HandleFunc to define which function will handle which API endpoint.
  3. With http.ListenAndServe we define the port that your program must listen to continuously.We wrap that around log.Fatal so that all exeptions are logged.

To run your code type the following in your console
go run main.go

If you face an error telling you that mux is not installed then run
go get -u github.com/gorilla/mux in your console.

Post Requests

Photo by Andrik Langfield on Unsplash

Let us now post some data to the server.

Note: Click here to know more about json in Go.

  1. Adding a new function and a function handler.

2.  Creating structs that will hold our json data.

3. Writing our add function.

Putting it all together

Testing it using postman

Hope this post helped you. If you want more help, feel free to ping me @Ashish_che

❌
❌