• Breaking News

    Thursday, September 2, 2021

    What does modern (good) API development look like and what are the best tools to use? Ask Programming

    What does modern (good) API development look like and what are the best tools to use? Ask Programming


    What does modern (good) API development look like and what are the best tools to use?

    Posted: 02 Sep 2021 11:22 AM PDT

    Let's say for a simple and straightforward RESTful API (mainly focused on CRUD operations) - how do companies & developers approach building these out efficiently and sustainably? Typically when I build out a hobby project, it's a very ramshackle approach starting with writing the endpoints by hands, say in Express.js, before going back and trying to quantify docs & specs - clearly not a sustainable approach. The more effective workflow I have in mind is:

    • Design the endpoints and specifications in Postman, Swagger, etc
    • Use this to generate an OpenAPI specification
    • Per the specification, or using a generation tool, pre-write the actual endpoints for the server-side application
      • Are there useful tools to expedite this? In a Node.js stack, I've seen swagger-node-codegen for example, but unsure if the buck stops there as far as abstracting the endpoints and interactions server-side for different types of databases, etc.
    • Add any specific logic per endpoint that goes beyond basic CRUD activity

    How does this workflow change in a serverless context (i.e., the endpoints are cloud / Lambda / etc functions or similar)? Are there standards around this? What are the best tools (plugins, npm packages, etc, beyond Postman & Swagger) for sustainable API development?

    submitted by /u/NewEnergy21
    [link] [comments]

    Confusion over the constants in GNU C manual...

    Posted: 02 Sep 2021 10:00 PM PDT

    Hello,

    Im a Beginner to C programming, not an Absolute Beginner though.

    I was reading the GNU C manual, which might be referring to variables as constants here.

    May I ask what is going on there.

    Also I found this, and would ask you, what should be used when, among "static const", 'const', '#define', and 'enum'.

    Thanking you...

    submitted by /u/wine-gamer
    [link] [comments]

    Building internal analytics tool in Python

    Posted: 02 Sep 2021 08:06 PM PDT

    Is there a good framework/library for building internal tracking/analytics tools other than the built-in logging library? My front-end will be making post requests to the backend and I want to record them in an efficient, robust way. I could simply open a file and write a line, but I think it would be more efficient if the logged data would live in memory for a little longer before being sent to disk. What is the standard practice in building internal tracking/analytics tools for Python?

    submitted by /u/FemaleBounds
    [link] [comments]

    I need help with a Collatz function

    Posted: 02 Sep 2021 07:36 PM PDT

    I need help with a Collatz function

    Hello, guy who just started here.

    I was asked to make a program in Python that would tell you the number of times a Collatz sequence had to be repeated until it finally reached the value of 1

    So 1 = 0 (because it's already 1), 2 = 1, 3 =7, etc.

    I would really appreciate if anyone's that familiar with the collatz sequence could help me!

    submitted by /u/Dd123456123456
    [link] [comments]

    How do I create a code to detect whether a user has selected an image file or a video file?

    Posted: 02 Sep 2021 07:22 PM PDT

    I want to show a preview of an uploaded video file before submit. I have successfully done this with an image thanks to the following JS, but it is not working with video files...

     const inpFile = document.getElementById("inpFile"); const previewContainer = document.getElementById("imagePreview"); const previewImage = previewContainer.querySelector(".image-preview__image"); inpFile.addEventListener("change", function() { const file = this.files[0]; if(file){ const reader = new FileReader(); previewContainer.style.display = "block"; previewImage.style.display = "block"; reader.addEventListener("load", function() { previewImage.setAttribute("src", this.result); }); reader.readAsDataURL(file); }else{ previewImage.style.display = null; previewImage.setAttribute("src",""); } }); 

    So I created another JS function for previewing video files before submit:

     const input = document.getElementById('inpFile2'); const video = document.getElementById('video'); const previewContainer = document.getElementById("videoPreview"); const previewVideo = previewContainer.querySelector(".video-preview__video"); const videoSource = document.createElement('source'); input.addEventListener('change', function() { const files = this.files || []; if (!files.length) return; const reader = new FileReader(); reader.onload = function (e) { videoSource.setAttribute('src', e.target.result); video.appendChild(videoSource); video.load(); video.play(); }; reader.onprogress = function (e) { console.log('progress: ', Math.round((e.loaded * 100) / e.total)); }; reader.readAsDataURL(files[0]); }); 

    This is my HTML:

     <div class="image-preview" id="imagePreview"> <img src="" class="image-preview__image" alt="Video selected"> </div> <div class="video-preview" id="videoPreview"> <video controls autoplay muted class="video-preview__video" id="video" src=""></video> </div> <br> <label for="inpFile"><img id="image_icon" src="images_icon.png"></label> <input id="inpFile" type="file" name="file" style="display:none;"> <label for="inpFile2"><img id="video_icon" src="images/video_icon.png"></label> <input id="inpFile2" type="file" name="file" style="display:none;" accept="video/*"> <input id="post_button" type="submit" value="Post" style="margin:5px;"> 

    I'm not sure how to create a code that can help detect whether the user has selected an image or video file. An example of this feature is used by Twitter. When you select an image/video to tweet, Twitter would let you preview the image/video in the textarea. That's the feature I want to implement into my website. Any help would be greatly appreciated!

    submitted by /u/RustyRice23
    [link] [comments]

    Who has bought a domain name but never used it?

    Posted: 02 Sep 2021 03:33 PM PDT

    What to put in an endpoint's response

    Posted: 02 Sep 2021 07:49 AM PDT

    I'm writing a couple end-points for a work project in python (using flask). When I'm returning a value (as part of a get method) its pretty intuitive what to put in the response body.

    Say I'm returning a username or something. I would respond a json body with {user_name: "ninjapanda"} or something.

    But I'm having trouble when it comes to what to put in the response body for errors. I was originally just returning a string with the description of the error, but my boss wants everything returned to be json through the flask response object.

    That's fine, but everything in json needs to have a key value pair like a dictionary.

    What do I put for the key part? I could technically put anything.

    {"Error": "error message ..."} or something, but the whole point of doing this key value pair thing is to make it searchable. So people can use the json body and do something with it.

    So if I'm doing that, is there some standard for what goes in the first part of that json?

    What would go there?

    submitted by /u/Dotaproffessional
    [link] [comments]

    Difficult to concentrate at home & office. Have to go to public spaces to code efficiently.

    Posted: 02 Sep 2021 10:12 AM PDT

    This isn't a technical question but more so about the activity of programming itself.

    I notice that it is really hard for me to get into "flow" when trying to program at home, or at my coworking office.

    So recently I have been going to my local public library and also coffee shops, where I can somehow program with intense focus for hours. There seems to be something special to public work spaces that gets me into programming mode. It feels as if a switch has been flipped in my head and I just sit down and start coding.

    I tried many times to do this at my coworking office or at home, but it just feels so hard. In fact, it's almost impossible for me to code for more than 20 minutes without being distracted by something. It sucks.

    I don't really enjoy going to the library or coffee shops to code. The chairs are crappy and I always end the session with a sore back due to the poor chair and having to keep my neck lowered to look at my laptop's screen, whereas at home and office I have external monitors that is much more ergonomic.

    But then perhaps the physical discomfort of public work spaces is actually conducive to focus? I have a perfectly set up working environment at my coworking space, with a fancy herman miller chair and a 27" 4k monitor, yet when I get there, I can't focus and just end up browsing dumb shit on the Internet.

    I would like to be able to get into programming "flow" whenever I want to, and not have to go to a public space to do it. Is there any tricks to do this?

    submitted by /u/Valachio
    [link] [comments]

    Help with partially hiding parameters in PHP

    Posted: 02 Sep 2021 03:13 PM PDT

    Hi. I am fairly new to programming and am writing a basic PHP application for a security challenge. The idea is to create an application that is vulnerable to HTTP Parameter Pollution. Essentially, when multiple parameters of the same name are passed in a GET/POST request, the last occurrence of the parameter is the one whose value sticks (in PHP at least - different technologies process this differently).

    What I am trying to do is have a page with parameterA hidden in the URL, but parameterB visible. The challenge would be for the user to add on a second parameterA with their own value, overriding the original parameterA.

    my application is a very basic social media site where a user is viewing a friend's profile. The URL contains both users' IDs. The goal is to spoof the user's ID parameter ('me' below) so that he can view the profile of someone he's not actually friends with. For example:

    The user clicks a friend in his contacts which takes him to the URL:

    mysite.com/viewprofile?friend=333

    In the backend, the actual URL is as follows, which the application uses to verify that both users are friends and pull the friend's profile info if so:

    mysite.com/viewprofile?me=111&friend=333

    the user would then edit the URL to:

    mysite.com/viewprofile?friend=444%me=222

    which would make the backend URL into:

    mysite.com/viewprofile?me=111&friend=444&me=222

    this would effectively set the 'me' parameter to 222 instead of 111, allowing the user to view user 444's profile, who he isn't friends with.

    I don't think it's possible to hide some parameters in the URL while showing others, so I'm wondering how I can achieve this. I think there needs to be some sort of server-side functionality that builds the URL and returns the resulting data the user, I'm just not sure how design this.

    Ignore the fact that a social media site would not work this way. Any help/resources would be great.

    TIA!

    submitted by /u/newaccountwhodis529
    [link] [comments]

    Where should I store the configuration data I often need to access

    Posted: 02 Sep 2021 03:27 AM PDT

    I have a API server and some end points use configuration data (weights for random data) in a file (few kB size) . The question is where I should store the data: constantly access file (probably not), load to program itself, in memory database (redis etc.), database (current progesql), some where elsee?

    submitted by /u/Johnees
    [link] [comments]

    Does open source software get hacked as often as closed source software?

    Posted: 02 Sep 2021 08:43 AM PDT

    Hi, I was curious about a phenomenon I noticed in the tech world where open source software seems to get hacked less often than closed source software.

    Is this an illusion on my part or is it really the case that open source is less vulnerable to attacks?

    submitted by /u/imstillmb
    [link] [comments]

    What is the best way to implement time-based locks on resource editing?

    Posted: 02 Sep 2021 02:25 AM PDT

    I am working on a full-stack application.

    I would like certain resources to be locked for editing a certain amount of time after a certain task has been completed, which would trigger the lock and after the specified time threshold is exceeded, it becomes read-only.

    Here are the two solutions I have in mind:

    • Add either a boolean or Date field to the locked row, and calculate whether the resource should be locked or not based on those.
    • Do that calculation on the fly every time you fetch the resource - This comes with the advantage that if I decide to change the threshold time for locking the resource I would not need to alter data in the database; but the nature of the check also causes a lot of extra database queries which I am not a fan of.

    Anyone here who's implemented this kind of thing before have any advice on what approach they took + any advantages / disadvantages?

    submitted by /u/LividProspect
    [link] [comments]

    Typo-tolerant search with these requirements?

    Posted: 02 Sep 2021 07:20 AM PDT

    The main feature of my project will be the search, so I am trying to do it right and want to return relevant results.

    I have a list of items that can be searched by (title, description, tags) and filtered by (categories, timeframe, popularity), and some more. Users can also like these items and they are added to their list of liked items. The likes needs to be returned with the search so that users see how many likes the items have, and also see if they already liked it. Think of something like Dribbble where you can see numbers of likes and see if you liked it already.

    Since I am already using Postgresql, I tried to implement typo-tolerant search with pg_trgm. Either I don't understand how to implement it properly or it just doesn't work the way I would like. As I understand it, it works best for short sentences like titles, but not for description fields with hundreds of words. There is also the filtering / sorting / ranking which can become tricky.

    This is why I was leaning towards Algolia, Elasticsearch, Typesense or Meilisearch. If you want to share some pros-cons of each, that would be great :)

    With these, I think my main issue will be the like counter. If someone likes an item, I have to increase the counter in the search engine. This can be costly if items are getting liked a lot, as the documents have to be reindexed every time.

    Are any of the above search engines suitable for these types of frequent updates?

    How would someone approach this? Would they just do things differently, like a batch update every few minutes?

    Also, for each results returned, I need to indicate if you already liked this item. Like Dribbble, the hearth is already red if it's in your list of liked items. If I am right, these liked items would not be indexed in the search engine, but rather on my Postgresql database. I imagine what would be best is if I just preload your liked items on first load and cross-check client-side on every search results to check if you already liked it.

    This is all so new to me and I am lost. How does Dribbble do it? Also, is it crazy to think of something like a search-as-you-type? That would be nice, but probably very resource hungry?

    One more thing.. It would be great if someone could search / filter their own list of liked items, but I understand that would create a lot of duplicate documents on the search engine. So for this part I was thinking about preloading their liked items and searching / filtering client-side using something like fusejs. Is that a good approach? Any ideas?

    Sorry, I know that's a lot of questions and it can be confusing. Hell, I'm confused by my questions myself!

    I guess my questions are:

    1. How to efficiently maintain a "likes" counter on a search engine like Algolia, Elasticsearch, Typesense, Meilisearch.
    2. Which search engine would you recommend?
    3. How to keep track of which items you already liked from the search results given by the search engine?
    4. How to search/filter your own list of liked items? Can't imagine keeping track of this on the search engine.
    5. Search-as-you-type makes sense with a "like" feature?

    I appreciate any help I can get.

    If it helps, I will be using VueJS + Golang.

    Thank you so much

    submitted by /u/RookieRaccoon
    [link] [comments]

    Dependency graph to layers of separately processable entries

    Posted: 02 Sep 2021 06:04 AM PDT

    So I have a acyclic dependency graph. I can easily create a 'single threaded' list of the entries such that all dependencies are before the things they depend on, just a simple topological sort does that.

    But, I also want to generate a 'parallel' list, where I can break it into separate layers (list of lists), the entries of each of which can be processed in parallel (because they are unrelated and all of their dependencies have been taken care of by the underlying lists.) And optimally so, i.e. every entry is at the lowest level it technically can be.

    AFAIK, topological sorts aren't going to provide that. I can think of some somewhat brute force methods involving generating the single threaded list and then making repeated passes through it. But I've not come up with or found a clever algorithm to do this sort of thing, though I'm sure it exists.

    Anyone know of such a beast?

    submitted by /u/Full-Spectral
    [link] [comments]

    Simple C Question - Increment and Decrement Operator

    Posted: 02 Sep 2021 05:27 AM PDT

    Hi guys, I have a simple snippet of a code here:
    int i = 3, j = 10;
    printf("%d\n", (i-- + ++j));
    printf("%d\n", i);
    printf("%d\n", j);

    Why does the 1st print give 14? Since (i-- + ++j) is in brackets, I assumed they would be done first, so ++j gives 11, and i-- gives 2. Then, adding them gives 13 and so the print is 13.

    submitted by /u/CardiologistLiving51
    [link] [comments]

    need to build API with existing database || what to learn?

    Posted: 01 Sep 2021 11:55 PM PDT

    I am an Android Dev, and never handled backend.

    I am willing to create REST API/webservice that will return data as JSON format for Mobile Application, with existing populated MS SQL Server database using Spring Boot.

    As I am working with Android java, I thought Spring Boot should be easy to catch for me.

    So, how can I prepare myself, what and what do I need to learn?

    submitted by /u/swordofgiant
    [link] [comments]

    No comments:

    Post a Comment